Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 572
Corey P Avatar asked May 04 '18 18:05

Corey P


People also ask

What is ModelSnapshot EF?

A Model Snapshot is the current state of the model stored in a class file named <YourContext>ModelSnapshot. cs The file is added to the Migrations folder when the first migration is created, and updated with each subsequent migration.

Can Core 3.1 Use EF Core 5?

EF Core 5.0 requires a . NET Standard 2.1 platform. This means EF Core 5.0 will run on . NET Core 3.1 or .


1 Answers

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

like image 178
M. Artem Avatar answered Oct 13 '22 09:10

M. Artem