'Update ContextModelSnapshot EF Core
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?
Solution 1:[1]
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
Solution 2:[2]
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:
- add new migration - this will sync snapshot, migration designer will be ok for future migration removing
- remove all commands from migration .cs file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Pangea |