The Model Snapshot In Entity Framework Core

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. It enables the migrations framework to calculate the changes required to bring the database up to date with the model.

Model Snapshot

In previous versions, this information was stored in the database, which resulted in the need to query the database prior to scaffolding the code for a new migration. It was also stored as part of each individual migration as a resource file (.resx) per migration. This caused problems in team environments where each developer had their own development database, often at different stages of development or were trying to apply different migrations to a shared development database at the same time.

When the snapshot was saved as part of each migration, it was possible to delete a migration, and in doing so, the snapshot was deleted, still leaving migrations and snapshots in sync with eachother. If you delete a migration file in EF Core, the snapshot and migrations will be left out of kilter. For this reason, you are advised to get yourself into the practice of only ever use the remove-migration command to revert a migration. Having said that, if you do inadvertently delete a migration file from the migrations folder, EF Core will recognise this and revert the snapshot for you.


Created:
Last updated: 14/03/2017 16:33:42
Proficiency Level: Beginner