2

I have two DbContexts in the database and want to make one of them only reference the one table (class):

dotnet ef migrations add Mall20200325 --context ApplicationDbContext

And in the ApplicationDbContext there is only one DbSet:

public DbSet<Models.User> Users { get; set; }

But it will still migrate other tables.

How to do I accomplish this?

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Angella Yu
  • 21
  • 4
  • Can you clarify what you’re looking for? Am I to understand that you want your two separate DbContexts, each mapped to a separate table? But you want to ensure other tables are still created as part of the migration, even though they’re not mapped to a DbContext? (I may well be misunderstanding; I’m extrapolating a bit.) – Jeremy Caney Mar 25 '20 at 06:53
  • @JeremyCaney thank you . I want the tables not mapped in the DbContext will not be migrated. – Angella Yu Mar 25 '20 at 07:05
  • 1
    If I use dotnete ef migrations add xxx ---conntext ApplicationDbContext.. some table not mapped in the ApplicationDbContext also have migrated. but they are mapped in another DbContext. I want to these tables are seperate. – Angella Yu Mar 25 '20 at 07:07
  • If you have other entities as navigation property in `Users`, then they are indirectly mapped by the datacontext. You'll have to either [Ignore](https://www.learnentityframeworkcore.com/configuration/fluent-api/ignore-method) those fields or include them in your context anyway. – Mat J Mar 25 '20 at 07:15
  • @MatJ. No,The Users is only mapped in ApplicationDbContext.Other tables such as Orders is in anther DbContext.But when I migrate the ApplicationDbContext it will also migrate the other tables including Orders. but it is not mapped in ApplicationDbContext – Angella Yu Mar 25 '20 at 07:43
  • Include the User class definition in your question. Are you sure your User class does not have any property with type `Orders` or such? – Mat J Mar 25 '20 at 08:24
  • @MatJ yes there is .It is my fault. and I use modelBuidler.Ignore with all other tables . – Angella Yu Mar 25 '20 at 09:01

3 Answers3

0

your problem its than you are using two -- instead one

dotnet ef migrations add Mall20200325 -context ApplicationDbContext

sGermosen
  • 312
  • 3
  • 14
0

I found the following solution from here. Assume you have AccountDbContext and ApplicationDbContext contexts. Assume in the AccountDbContext the entity Account has been defined and we want to exclude accounts table from migration, while we have a reference to it in the User entity. Therefore:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    // Ignored table `accounts` from other DbContext in the migration

    modelBuilder.Entity<Account>(entity => {
        entity.ToView("accounts");
        // If the `acoounts` table has some columns you must introduce their
        // names like this (if the properties' names are different from columns' names)    
        entity.Property(e => e.Id)
              .HasColumnName("id");
        entity.Property(e => e.FirstName)
              .HasColumnName("first_name");
        entity.Property(e => e.LastName)
              .HasColumnName("last_name");
    });
    
    // Define the table `users`

    modelBuilder.Entity<User>(entity => {
        entity.ToTable("users");

        entity.HasIndex(e => e.Id)
           .HasName("users_account_id_foreign");

        entity.Property(e => e.Id)
           .HasColumnName("id")
           .HasColumnType("bigint(20) unsigned");

       entity.HasOne(d => d.Account)
           .WithOne(p => p.Users)
           .HasForeignKey<User>(d => d.Id)
           .HasConstraintName("users_account_id_foreign");
        
    });

}
MJBZA
  • 4,796
  • 8
  • 48
  • 97
0

The Add-Migration process compares two IModel instances, one from your context's OnModelCreating the other compiled into your assembly and loaded from the IMigrationsAssembly service.

The MigrationsAssembly implementation should only consider model snapshots and migrations with a matching DbContextAttribute.

If you are seeing migration operations that you didn't expect, then I have to wonder what your project looked like before you created two contexts. Do you have a generated ModelSnapshot, with a mismatched [DbContext] attribute?

Jeremy Lakeman
  • 9,515
  • 25
  • 29