title: Entity Framework Core - Multiple DbContext single database
body: <p>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 <code>DbContext</code> contains lots of tables (to cover all the versions) and i want to separate this one in smalls many <code>Dbcontext(s)</code></p>
  
  <p>So What is the best approach ? 
  I would have <code>DI /IOC</code> to resolve a <code>CurrentContext</code> (return <code>DBVersion1</code> or <code>DBVersion2</code> or <code>DBVersion3</code> according to the type of version (set by the user when launching UI)
  Is the code below is the good approach ? </p>
  
  <pre><code>public class DBVersion1 : DbContext
  {
      public DbSet&lt;Blog&gt; Blogs { get; set; }  
      public DbSet&lt;Post&gt; Posts { get; set; }
      public DBSet&lt;User&gt;{ get; set; }
      public DBSet&lt;Books&gt; { get; set; }
      public DBSet&lt;Engine&gt; { get; set; }
  }
  
  public class DBVersion2 : DbContext
  {
      public DbSet&lt;Blog&gt; Blogs { get; set; }  
      public DbSet&lt;Post&gt; Posts { get; set; }
      public DBSet&lt;User&gt;{ get; set; }
      public DBSet&lt;Books&gt; { get; set; }
      public DBSet&lt;Engine&gt; { get; set; }
      public DBSet&lt;Paiement&gt;{ get; set; }
      public DBSet&lt;Invoice&gt; { get; set; }
  }
  
  public class DBVersion3 : DbContext
  {
      public DbSet&lt;Blog&gt; Blogs { get; set; }
      public DbSet&lt;Post&gt; Posts { get; set; }
  }
  
  public static DbContext CurrentContext
  {
      get
      {
      var _servicesCollection = new ServiceCollection().                    
      AddDbContext&lt;DBVersion1 &gt;(options =&gt; options.UseSqlServer(@"Server=
   (localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"))
      .AddDbContext&lt;DBVersion2 &gt;(options =&gt; options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"))
      .AddDbContext&lt;DBVersion3&gt;(options =&gt; options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"))
      .BuildServiceProvider();
  
      return _mainContext ?? (_mainContext = _servicesCollection.GetService&lt;DBVersion1&gt;());  
              }
          }
  </code></pre>
  
  <ul>
  <li>How can i switch between <code>Dbcontext1/2/3</code> in <code>CurrentContext</code> <code>getter</code> ?</li>
  <li>Refactoring the actual may be difficult ? ie today the viewmodel's can have 
  <code>var v = DBContext.Paiement....</code> et</li>
  </ul>
  
  <p>after : 
      <code>var v = CurrentContext.Paiement</code> ? => does not compile in the case of Version1 ? (or maybe it's no sens in this case ?)</p>
  
  <p>Thanks</p>