2

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

6
  • Once you resolve your different context and get your different data, what then? The differences between versions usually span all the way from data to view in large apps I've worked on. Commented Apr 25, 2018 at 7:54
  • In EF terms, DbContext is the equivalent of database. If you have single database, then you should have single context. Anything else will just cause you a problems in one or another way. Commented Apr 25, 2018 at 8:05
  • Or create one context for the stable part of the data model and use something else (for example Dapper) for the rest. But the real problem is in the last part of your question. Your app should be able to handle all possible class definitions, while only some of them are supported. That requires some sort of abstraction layer. How to do that is another question. Commented Apr 25, 2018 at 8:52
  • "i want to separate this one in smalls many Dbcontext(s)" -> can you explain why you would do that? What advantages would it give you? Why not keep 1 version of the dbcontext, and work with bounded contexts in the domain layer instead to achieve this separation? Commented Apr 25, 2018 at 8:53
  • @IvanStoev : Yes, you're right ...so , maybe my point of view is bad, and a better approach is to have 2 or 3 databases (with different schemas) according to the app' version ? Commented Apr 25, 2018 at 9:00

1 Answer 1

2

"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.

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

1 Comment

Splitting the database to different DbContext's could be a good idea if you want to have one DbContext per bounded context in your application. You could use different schemas for each bounded context. Since you are paying per db and not per server on Azure this is definitely a sane approach sometimes. However, mapping the same table to multiple DbContext's is just madness...

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.