I am working on an ASP.NET application that can interact with several database managers. So I decided to use entity-framework to successfully inter-act with these database managers.Currently my problem is that when I want to switch from one database to another I am forced to modify the constructor of the "DbContext". For example, to use a database under SqlServer here is what my ApplicationContext class looks like
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; }
}
To access a MySql database, here is what my ApplicationContext looks like
[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; }
}
To use my ApplicationContext class I use a data access layer. Here is an example
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();
What I would like to do is create an ApplicationContext file for each database manager so that you can switch dynamically according to the choice of the user.
Thank you in advance for your answers