1

I am looking at some code online where there are two Contexts for the same database (one for read and one for write):

public class OrderReadContext: DbContext
    {
      public OrderReadContext() : base("name=GeekStuffSales") {

      }
      public DbSet<SalesOrder> Orders { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
      modelBuilder.HasDefaultSchema("Order");
   }
  }
  public class OrderSystemContextConfig : DbConfiguration
  {
    public OrderSystemContextConfig() {
      SetDatabaseInitializer(new NullDatabaseInitializer<OrderReadContext>());
    }

  }

Ladislav Mrnkas' answer to this post explains that the initializer must be set to null in sub contexts: Entity Framework: One Database, Multiple DbContexts. Is this a bad idea?

Why must the Database Initializer be set to null in a sub context?

I tried adding a seed method to one of my sub contexts, however this causes an error saying there are outstanding migrations. Is this not allowed?

w0051977
  • 15,099
  • 32
  • 152
  • 329

1 Answers1

1

Actually you can have as many context as you need on the same database (Migration history table has a reference to the context).
If the contexts shares tables you have to make some fixes to migrations (EF tryes to create the same table more times). This is probably your case in this is why you need to disable initializer.

If this is not your case (tables overlapped), you can see a sample here but you won't see anything interesting, it just works.

https://github.com/bubibubi/JetEntityFrameworkProvider/tree/master/JetEntityFrameworkProvider.Test/Model37_2Contexts

In this case the tables are not overlapped so I just create the tables using the standard initializer.
These are the queries runned over Migration history table

insert into [__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
values ('201709210714271_InitialCreate', 'JetEntityFrameworkProvider.Test.Model37_2Contexts_1.Context1',  0x1F8B08000...and so on... , '6.1.3-40302');
insert into [__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
values ('201709210714279_InitialCreate', 'JetEntityFrameworkProvider.Test.Model37_2Contexts_2.Context2',  0x1F8B0...and so on... , '6.1.3-40302');
bubi
  • 6,414
  • 3
  • 28
  • 45
  • Are you saying that you cannot use Seed methods if one table exists in multiple contexts? +1 for the reference to the Migrations table. Thanks. – w0051977 Sep 21 '17 at 07:30
  • I'm not sure I've understood. The issue is only on table creation and table migration. If you have 2 contexts with the same entities (same poco classes) and the same tables (same database, same schema and same table), when you create the database for the 2 contexts or you change the entities, the tables with standard migration configuration is runned 2 times, one for each context (so on the second create table you will receive an error). So, in your case you can enable only one default migration. On that migration you can also Seed tables. – bubi Sep 21 '17 at 07:50