Entity Framework Core - Multiple DbContext single database

c# dbcontext entity-framework entity-framework-core wpf

Question

I have an wpf app + EF Core 2 to access Data (several tables in db) The software is plan to be in 2 or 3 differents "versions" (depending on the business, but will share the same db and the "same ui" with various views). Today the DbContext contains lots of tables (to cover all the versions) and i want to separate this one in smalls many Dbcontext(s)

So What is the best approach ? I would have DI /IOC to resolve a CurrentContext (return DBVersion1 or DBVersion2 or DBVersion3 according to the type of version (set by the user when launching UI) Is the code below is the good approach ?

public class DBVersion1 : DbContext
{
    public DbSet<Blog> Blogs { get; set; }  
    public DbSet<Post> Posts { get; set; }
    public DBSet<User>{ get; set; }
    public DBSet<Books> { get; set; }
    public DBSet<Engine> { get; set; }
}

public class DBVersion2 : DbContext
{
    public DbSet<Blog> Blogs { get; set; }  
    public DbSet<Post> Posts { get; set; }
    public DBSet<User>{ get; set; }
    public DBSet<Books> { get; set; }
    public DBSet<Engine> { get; set; }
    public DBSet<Paiement>{ get; set; }
    public DBSet<Invoice> { get; set; }
}

public class DBVersion3 : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }
}

public static DbContext CurrentContext
{
    get
    {
    var _servicesCollection = new ServiceCollection().                    
    AddDbContext<DBVersion1 >(options => options.UseSqlServer(@"Server=
 (localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"))
    .AddDbContext<DBVersion2 >(options => options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"))
    .AddDbContext<DBVersion3>(options => options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"))
    .BuildServiceProvider();

    return _mainContext ?? (_mainContext = _servicesCollection.GetService<DBVersion1>());  
            }
        }
  • How can i switch between Dbcontext1/2/3 in CurrentContext getter ?
  • Refactoring the actual may be difficult ? ie today the viewmodel's can have var v = DBContext.Paiement.... et

after : var v = CurrentContext.Paiement ? => does not compile in the case of Version1 ? (or maybe it's no sens in this case ?)

Thanks

1
1
4/25/2018 7:59:23 AM

Popular Answer

"I want to separate this one in smalls many Dbcontext(s)"

This may not be an answer that solves your question on how to do that, because I don't believe splitting a db context would be a good practice.

Your DB context represents the database. The entire database, not a part of it. If you split this, you will most likely run into serious issues. For example, you update something with version 1 of the context, while version 2 of the context still has old data cached to work with.

I'm also very worried about the maintenance nightmare you would introduce with this separation. After all, you would have to maintain multiple db contexts now, and each one of them probably share some parts as well.

So the main question is: why would you want to do that? What are the advantages? And is it worth it, if you look at the risks?

A better solution, in my opinion, would be to have 1 DB context, and make the split on the level on top of it, for example, in the domain layer (bounded contexts). Then you have a logical separation instead of a physical one, which makes more sense.

If you really want a physical split, then split the database itself as well. But mind referential consistency between keys, you introduce complexity like this...

Hope this helps you.

1
4/25/2018 9:23:36 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow