私はいくつかのデータベースマネージャとやりとりできるASP.NETアプリケーションを開発中です。そこで、私はentity-frameworkを使用して、これらのデータベースマネージャとの相互作用を成功させることにしました。現在、私の問題は、あるデータベースから別のデータベースに切り替えるときに、 "DbContext"のコンストラクタを変更することです。例えば 、ここでのSQLServerの下でデータベースを使用するためには、私のApplicationContextクラスは、ここでは、MySQLデータベースにアクセスするにはEntityフレームワークを使用した動的マルチデータベースコンテキスト
public class ApplicationContext : DbContext
{
public DAOContext() : base("name=SqlServerContext")
{
}
public DbSet<Client> Clients { get; set; }
public DbSet<Commande> Commandes { get; set; }
public DbSet<Produit> Produits { get; set; }
public DbSet<LigneCmd> LignesCmd { get; set; }
}
のように見える私のApplicationContextが私のApplicationContextクラスを使用するには
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class DAOContext : DbContext
{
public DAOContext() : base("name=MySqlDAOContext")
{
}
public DbSet<Client> Clients { get; set; }
public DbSet<Commande> Commandes { get; set; }
public DbSet<Produit> Produits { get; set; }
public DbSet<LigneCmd> LignesCmd { get; set; }
}
のように見えるものは何かということです私はデータアクセス層を使用します。ここでは例
public class ProduitsDAL
{
public ApplicationContext db { get; set; }
public ProduitsDAO()
{
DbInit();
}
public void DbInit()
{
db = new ApplicationContext();
}
public List<Produit> ListProduits()
{
List<Produit> Produits = new List<Produit>();
Produits = db.Produits.OrderBy(p => p.ProduitId).ToList();
return Produits;
}
}
ProduitsDAL ProduitsManager = new ProduitsDAL();
List<Produit> ListProduits = new List<Produit>();
ListProduits = ProduitsManager.ListProduits();
私は何をしたいのは、ユーザーの好みに応じて動的に切り替えることができるように、各データベース・マネージャのApplicationContextのファイルを作成です。
public abstract class ApplicationContext : DbContext
{
public ApplicationContext(string cxnStringName) : base("name="+cxnStringName)
{
}
public DbSet<Client> Clients { get; set; }
public DbSet<Commande> Commandes { get; set; }
public DbSet<Produit> Produits { get; set; }
public DbSet<LigneCmd> LignesCmd { get; set; }
}
し、各特定のプラットフォームのためのコンテキストを導き出す:
はあなたが両方のコンテキストの基本クラスを設定することができ、あなたの答えを事前に
ソリューションありがとう – Younz301