1

I am using EF Core on .NET 6 with a code-first approach.

I want to have two contexts that connect to the same database. I want them each to have an Items property.

However, in one of them the Items property is mapped to the table in the database in one zone, while in the other in the other zone. E.g. zone1.Items and zone2.Items.

The tables have different columns.

So I have Context1 defined as:

public class Context1: IdentityDbContext<User>
{
    public DbSet<ItemOld> Items { get; set; }
    // ...
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<ItemOld>().ToTable("Items", "zone1");
        //...
    }
}

and similar in Context2 for ItemNew and zone2.

If I want to make a change on the ItemOld class (e.g. add a property), in Package Manager Console I do something like:

Add-Migration AddedSomeProperty -Context Context1 -o MigrationsContext1

This adds a migration in my folder as expected, and creates the Context1ContextModelSnapshot.cs file in that same folder.

I run Update-Database -Context Context1 to update my database.

But then, if I change something on the ItemNew class and try to do:

Add-Migration AddedSomePropertyOnItemNew -Context Context2 -o MigrationsContext2

EF Core will (as per my understanding and my situation):

  1. Look at the existing ContextModelSnapshot for that context
  2. Find all the differences from the current database to the info in the current ContextModelSnapshot
  3. Create the migration

Hence, it will also pull the code needed to add a property to ItemOld, as the fact that it was already added was not available in the Context2ContextModelSnapshot.

So, basically, I have a ContextModelSnapshot for each context and they differ if I add migrations to each context.

An idea would be to have both contexts use the same ContextModelSnapshot, but I could not find a way to configure this - is there a way to do that?

Is there a better way to approach this problem?

Please close me down if the question is a bit vague, or let me know if I need to provide additional information.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mario Mucalo
  • 597
  • 5
  • 16
  • The implementation of `IMigrationsAssembly` https://github.com/dotnet/efcore/blob/release/7.0/src/EFCore.Relational/Migrations/Internal/MigrationsAssembly.cs filters based on `[DbContext]` attributes. You could replace this service. You can also have the same table in multiple models, and `.ExcludeFromMigrations` for all but one of them. https://stackoverflow.com/questions/22038924/how-to-exclude-one-table-from-automatic-code-first-migrations-in-the-entity-fram – Jeremy Lakeman Aug 18 '22 at 03:33
  • @JeremyLakeman replacing `IMigrationsAssembly` could technically do the trick, but even EF Core advises not to do so. Regarding `.ExcludeFromMigrations`, I don't think this can help me. – Mario Mucalo Aug 18 '22 at 05:25
  • From your question; "Hence, it will also pull the code needed to add a property to ItemOld" This can only happen if `Context2` has that property in its model, and that class is not excluded. In other words, each table of your database should only be modified by the migrations for a single context. – Jeremy Lakeman Aug 18 '22 at 05:31

0 Answers0