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):
- Look at the existing
ContextModelSnapshot
for that context - Find all the differences from the current database to the info in the current
ContextModelSnapshot
- 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.