In EF Core 10, two branches of a car-rental API each add a migration; what breaks after both merge, and how is it fixed?
answer
- migrations form one ordered sequence
- each Designer freezes its own branch's model
- no latest-migration id in the 10 snapshot
- duplicate operations in the next add
- abort, remove yours, merge, re-add
basics
~20 sEach branch's migration was scaffolded from a snapshot that lacks the other branch's change, so the merged sequence carries inconsistent metadata. Fix it by setting your migration aside, merging the other branch, and scaffolding your migration again on top.
solid answer
~40 sEF Core 10 orders migrations by their timestamp id, and each migration's Designer file freezes the model of the branch it was created on. After both branches merge, the later migration's `BuildTargetModel` lacks the other branch's change. If Git conflicted on `RentalDbContextModelSnapshot.cs` and someone kept one side, the next `migrations add` scaffolds the other branch's tables or columns again, which fails when applied. If Git merged the snapshot cleanly, the damage can stay hidden until a later `migrations remove` restores an incomplete snapshot. EF Core 10's snapshot records no latest-migration id, so a conflict is not guaranteed. The fix: go back to the pre-merge state, remove your unpublished migration while keeping the model change, merge the other branch, and run `migrations add` again. Renaming or re-timestamping files does not fix the metadata.
code
bash · 10 linesgit merge --abort
# keep the model change, drop the generated migration
dotnet ef migrations remove
git commit -am "Drop AddRentalFuelPolicy migration before merging main"
git merge origin/main
# re-scaffold on top of the merged snapshot
dotnet ef migrations add AddRentalFuelPolicygo deeper
Remember that migrations form one ordered sequence and that the snapshot and Designer files must be committed with the migration that changed them.
Explain why each Designer file captures only its own branch's model, what the next add does after a one-sided snapshot merge, and the abort, remove, merge, re-add workflow.
Show you can spot a silent divergence when Git reports no conflict, know when remove is unsafe after a merge, and keep the shared history consistent with every applied database.
Decide how a team serializes schema changes: who owns migrations on a busy model, when to merge main before scaffolding, and what review gate catches parallel migrations.
## Why parallel migrations are a problem EF Core migrations are not independent SQL files. They form **one ordered sequence**, sorted by the timestamp id each migration gets when it is scaffolded, and every migration carries model metadata: - the **Designer file** (`<id>_Name.Designer.cs`) freezes the full model as it stood right after that migration, in `BuildTargetModel`; - the **model snapshot** (`RentalDbContextModelSnapshot.cs`) holds the model as of the latest migration and is what the next `dotnet ef migrations add` diffs against. Picture a car-rental API. Developer A adds a `Branch.OpeningHours` column on one branch and scaffolds `AddBranchOpeningHours`. Developer B adds a `Rental.FuelPolicy` column on another branch and scaffolds `AddRentalFuelPolicy`. Each migration was computed from a snapshot that did not contain the other developer's change. ## What breaks after both merge 1. **The snapshot conflict is resolved the wrong way.** If Git reports a conflict in the snapshot and the person merging keeps one side, the snapshot now lacks the other branch's column. The next `migrations add` sees that column in the model but not in the snapshot and scaffolds it **again**; applying that migration fails because the column already exists. 2. **The snapshot merges cleanly, but the metadata is still wrong.** The later migration's `BuildTargetModel` does not contain the earlier branch's change. Nothing fails today, but `dotnet ef migrations remove` rebuilds the snapshot from the previous migration's metadata, so a later remove restores a model that silently drops the other branch's work. 3. **Both migrations touch the same table or column.** Each was correct on its own branch; together they may conflict, for example two different renames of one column. On EF Core 10 the snapshot holds only the model, with no field naming the latest migration, so two branches that change different entities usually merge **without any Git conflict**. The divergence is real even when Git is silent. ## How to resolve it The documented workflow re-creates your migration on top of the other one: 1. Abort the merge and return to your pre-merge working tree. 2. Remove your migration while keeping the model change that produced it. If it is unapplied, `dotnet ef migrations remove` does it; if your local database already ran it, revert that database first (or use `--force`). Restoring the migration files and the previous snapshot from source control works too. 3. Merge the other branch. 4. Run `dotnet ef migrations add` again, so your migration is diffed against a snapshot that contains both changes and gets a Designer file that does too. ## What not to do | Tempting shortcut | Why it fails | |---|---| | Rename or re-timestamp your files so they sort after the other migration | Order changes, but your Designer metadata still lacks the other branch's change | | Run `migrations remove` after the bad merge is committed | It restores the previous Designer's model, which may lack the other branch's change | | Hand-merge both `Up` bodies into one migration | Databases that already applied either migration no longer match the history | | Hand-edit the snapshot until the next add looks empty | The snapshot and the Designer files disagree, and the next remove undoes your edit | ## Keeping it from happening - Coordinate schema work: two people changing the model on parallel branches is the root cause. - Merge the main branch into a feature branch before scaffolding its migration, not after. - Treat any migration that touches a table another open branch also changes as a review flag. - Remember that an unapplied migration with an earlier id is still applied when the database is updated, so a dev database can end up with a mixed order that the merged code never tested. ## Spotting a divergence in review - **Two migrations in one merge** whose timestamps interleave with migrations already on main. - **A snapshot diff that contains more than its migration explains**, such as columns the pull request never touched. - **A scratch `dotnet ef migrations add` after the merge that is not empty.** On a consistent sequence, adding a migration right after a merge with no model change scaffolds empty `Up` and `Down` bodies; anything else means the snapshot and the model disagree. An empty result does not prove the Designer metadata is right, though; it only rules out a one-sided snapshot. Delete that scratch migration with `migrations remove` once you have read it.
- Your local database already applied your branch's migration before you re-create it; what changes in the workflow?`dotnet ef migrations remove` will refuse because the history table lists the migration. Revert that local database to the previous migration first, or run `dotnet ef migrations remove --force`, which runs `Down` and then deletes the files. After re-adding, the migration gets a new id, so the database applies it as a new step.
- Why doesn't merging cleanly in Git prove that two EF Core 10 migrations are compatible?Git compares text. Two snapshots that changed different entity blocks merge without conflict, yet each Designer file still holds only its own branch's model, and the two migrations can still collide on one table. EF Core 10 has no snapshot field that forces a conflict, so the check has to be the team's.
- If Git merges the model snapshot without a conflict, the migrations are consistent.
- Renaming a migration file to a later timestamp fixes a diverged migration history.
- Running migrations remove after the bad merge restores both branches' changes.
- Each migration is an independent SQL file, so branch order does not matter.
- Hand-editing the snapshot is the supported way to resolve a merge conflict.
Assumes EF Core 10: its model snapshot does not record the latest migration id, so Git can merge two branches' snapshots with no conflict; the docs describe that id as an EF Core 11 addition.