Sergey Slepov
Sergey Slepov

Reputation: 2111

Why Migration.BuildTargetModel has so much duplicate code?

I'm setting up EF Core 3.1.3 migrations against an existing SQL Server database.

This is what I've done so far:

  1. Scaffold my model from the existing database.
  2. Add the InitialCreate migration.
  3. Remove the contents of the Up() method in InitialCreate.
  4. Update database to create the __EFMigrationsHistory table.

Then I added a property to an entity class and ran dotnet ef add migration.

Now looking at the BuildTargetModel method of the second migration I see that it contains pretty much the same code as MyDbContextModelSnapshot.BuildModel. I.e. it lists all entities and all their properties and relationships.

What does BuildTargetModel on a migration do? Why does it have to duplicate most of the snapshot code? I would only expect to find the diff in a migration.

Upvotes: 3

Views: 1077

Answers (1)

Sergey Slepov
Sergey Slepov

Reputation: 2111

As confirmed by the developers of EF Core, this behaviour is by design:

https://github.com/dotnet/EntityFramework.Docs/issues/2288

Upvotes: 2

Related Questions