Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 365
Sergey Slepov Avatar asked Feb 28 '26 09:02

Sergey Slepov


1 Answers

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

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

like image 112
Sergey Slepov Avatar answered Mar 03 '26 14:03

Sergey Slepov