Update ContextModelSnapshot EF Core
Asked Answered
A

4

45

I'm newer to EF Core, so please forgive me if there is a trivial answer.

Take this scenario....

A new web-application is being developed using the latest version of ASP.NET Core, with a code-first approach. At some point, the ContextModelSnapshot became out of sync with the database, due to deletions or source control. We cannot delete the database and recreate it.

Question: How can the snapshot be 're-synced' with the database?

Alamein answered 4/5, 2018 at 18:33 Comment(0)
B
58

You can execute the command

Add-migration temporary

to create a new empty migration. Then, run

Remove-Migration temporary (or their dotnet-cli counterparts)

In recent editions of EF Core (3+), just use:

Remove-Migration (will revert the last migration)

It will create model snapshot from scratch even if the migration has already been deleted. This approach works perfectly for Ef core 2.2.0-rtm-35687

Balsa answered 1/3, 2019 at 18:27 Comment(6)
pretty unexpected way to recreate snapshot from database, but worksFarris
This doesn't work because Add-Migration cannot be run without a migration name.Strother
Give it a name with Add-Migration MyName The name is not important because Remove-Migration will remove the file.Krieg
Remove-Migration takes no argument, at least in the recent releases of EF Core.Picklock
I am quite puzzled by "It will Revert model snapshot". Did @m-artem mean "It will recreate it from scratch"? Since it was out of sync, I don't see how it could be "reverted" so I assume it's "recreated".Sparke
Remove-Migration reverts the changes in the snapshot (EF Core 6). It's very confusing.Neoprene
G
1

This is caused by merging multiple migrations from different branches + remove migration after merging. After merge snapshot is Ok, but migrations designers are not synced.

If at any point you do remove migration, snapshot is recreated from latest applied migration designer. If designer doesn't contain all previous changes, from that point snapshot is not synced. When snapshot is not synced, even if you don't have changes in entity model, on adding new migration instead of empty migration you will have migration with already applied changes (but snapshot and migration designer are OK after new migration).

So if you are sure that all changes from entities are applied, solution is to add empty migration:

  1. add new migration - this will sync snapshot, migration designer will be ok for future migration removing
  2. remove all commands from migration .cs file.
Grussing answered 27/4, 2022 at 22:42 Comment(0)
W
0

for EF Core 8.0.1 (slight difference to existing answers)

  1. Delete ApplicationDbContextModelSnapshot.cs if it present

  2. Delete out of synch migration

  3. Comment out most recent changes in ApplicationDbContext

  4. check solution builds

  5. Add-Migration temp

  6. Delete temp migration file

  7. then uncomment most recent changes in ApplicationDbContext and run Add-Migration ... as usual

Wimble answered 4/5 at 21:44 Comment(0)
H
-10

it is so simple just remove ContextModelSnapshot then add new migration

Hadsall answered 20/3, 2021 at 15:44 Comment(1)
This is incorrect. If you delete the ContextModelSnapshot and add a new migration, then the new migration will contain ALL the instructions from the previous migrations (making the new migration redundant --and even conflicting-- to the former ones)Sparke

© 2022 - 2024 — McMap. All rights reserved.