1

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

1 Answer 1

3

You could set up a base class for both contexts:

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; }
}

And then derive a context for each specific platform:

class SqlServerContext : ApplicationContext
{
    public SqlServerContext() : base("SqlServerContext")
    {
    }
}

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
class MySqlContext : ApplicationContext
{
    public MySqlContext() : base("MySqlDAOContext")
    {
    }
}

Then in your DbInit function you can switch between context types. If your model is updated then both contexts get the updates and any common context code like fluent model building goes in the ApplicationContext base class. Note: I've marked the base class as abstract to force the user to use one of the derived types.

Now are you using migrations on both sql server platforms? It may seem annoying to still have two separate contexts but because they are different platforms you will eventually require separate migrations to support the differences between them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.