-2

I'm working with EF Core and I decided split the DbContexts I'm using. Everything is right but I notice that when I create the migrations for each context, EF tries to create the tables again even if the other context had created that table previously. I think EF Core must be trying to update the tables instead of trying to create them but I don't know why EF Core would do that.

AccountsContext :

 public class AccountsContext : BaseContext<AccountContext>
 {
        public AccountContext(DbContextOptions<AccountsContext> options): base (options){}
    public DbSet<Account> Accounts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Account>().HasKey(a => a.id);
        modelBuilder.Entity<Account>().Property(a => a.Name).HasField("_name");
        modelBuilder.Entity<Account>().Property(a => a.Role).HasField("_role");
        modelBuilder.Entity<Account>().OwnsOne(a => a.Password);
        modelBuilder.Entity<Account>().OwnsOne(a => a.Email);
    }
}

CampaignContext :

public class CampaignContext: BaseContext<CampaignContext>
{
      public CampaignContext(DbContextOptions<CampaignContext> options): base (options){}

    public DbSet<Campaign> Campaing { get; set; }
    private DbSet<Account> Accounts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Campaing>().ToTable("Campaings");
        modelBuilder.Entity<Campaing>().HasKey(a => a.Id);
        modelBuilder.Entity<Campaing>().HasOne(c => c.Owner);
        modelBuilder.Entity<Account>().ToTable("Accounts");
  }
}

Here is the scaffold of the migration in the campaign context. EF Core looks like its trying to create the accounts table even if it already exists in the database.

enter image description here enter image description here

Turner Bass
  • 801
  • 8
  • 20
Alejandro Mora
  • 157
  • 3
  • 16
  • I based in this example https://msdn.microsoft.com/en-us/magazine/jj883952.aspx but ported in ef-core because i want to implement bounded contexts suggested in DDD – Alejandro Mora Feb 12 '18 at 02:38
  • Also see https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question –  Feb 12 '18 at 05:48
  • Hi thanks for your observations Amy i always post the code as text but how i think that my problem is not the code so i think the problem is how ef-core is doing the migration I thought that in this ocation was no necesary but i make the change. – Alejandro Mora Feb 12 '18 at 15:52
  • Forget about "bounded" contexts. `DbContext` is EF (Core) equivalent of a database. Single database - single `DbContext`. All attempts (hacks) to avoid that will just cause you troubles. – Ivan Stoev Feb 13 '18 at 01:31

2 Answers2

0

you should specify the Context you want to use by

dotnet ef Update Database -Context DbContext

Danish Shaikh
  • 161
  • 1
  • 5
0

each context in Ef core considered as a separated database so you are putting that table in both of the databases I don't know why you are trying to do that but it is normal that you see the table in both migrations

Mustafa Mohamed
  • 195
  • 2
  • 8
  • yes i understand each db context represent a database but i want to do something like Jon P Smith describe [here](https://github.com/JonPSmith/EfCore.GenericBizRunner/wiki/Using-multiple-DbContexts) in the example you can see what i mean the posibility of share a table in different context. – Alejandro Mora Feb 05 '20 at 22:03
  • found a similar issue here is link to it this might help u solve your problem [link](https://stackoverflow.com/questions/49986756/efcore-map-2-entities-to-same-table) – Mustafa Mohamed Feb 05 '20 at 23:11