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.