Entity Framework Core Mutiple DbContext Migrations

2019-07-18 07:39发布

问题:

In Entity Framework 6 you could specify a ContextKey for a DbContext so that when using one database for multiple DbContexts the Migrations table was aware of what migration targeted what DbContext.

I've spent the last few hours trying to work out how you achieve the same in Entity Framework Core but haven't worked it out and documentation is lacking.

The new Migrations table in EF Core only has two columns - MigrationId, ProductVersion.

I thought HasDefaultSchema was the answer but doesn't seem to be.

Ideas?

回答1:

Previously, Entity Framework stored a snapshot of the model in the database. This resulted in Entity Framework having to query the database each time in order to work out what migrations had already run.

In EF7 migration history is stored in a snapshot in code (along with the existing migrations stored in the familiar Migrations folder).

Each time you update the model and create a migration the snapshot file gets updated. Therefore, there is no need for the ContextKey which was used in earlier version of Entity Framework.

Lovely.