Reputation: 1057
I've been using Entity Framework migrations for some time and I started wondering how they actually work under the hood. I mean the following:
How does EF understand that the model in the application and the db scheme are different? As far as I know, there's a table __MigrationHistory in the database where all migrations are stored as well as in the Migrations folder in the application (applies only to code-based migrations). In the __MigrationHistory table, there's a column called model which contains some kinda hash. What is that hash exactly? Is it a snapshot of the model? Is it the changes that EF needs to apply to get from the previous migration to this one?
If it was the model snapshot, it would mean EF would have to figure out how to transform the current model to the snapshot each time we decided to update database.
However, if it was the changes, it would mean EF would have to apply those changes to the current model in order to understand when the db model and the application model are different.
The question is, where can I read something about how the migrations are implemented and what is this model column in the database. I would appreciate any advices or links.
Update:
I've checked the resources provided and as I've found out, the model column is actually the snapshot of the model. That means, when I run the update-database command, EF goes to the db, checks the latest migration model by decoding the XML string and if the current model in the application and the model EF got from the db are different, EF generates a script to update the db. However, I still don't know what EF does when there are multiple migrations pending.
I will describe on example based on the example from the https://channel9.msdn.com/Blogs/EF/Migrations-Under-the-Hood. Say, we have First migration in your db, then Second migration, which adds the Url column and the Third one, which drops this column. If I were to apply these changes to the db which contains the schema similar to First, would EF add the column and then drop it according to the Second and Third, or will it try to calculate the general changes that are required to update the db and then execute the generated script (in the example case it would do nothing?
Also, there's another link I've found if anyone's interested https://msdn.microsoft.com/en-us/data/dn481501.aspx
Upvotes: 4
Views: 1305
Reputation: 2770
This Channel 9 video covers the general concepts. This blog post might be more specific to your question. In particular, the author concludes on the use of model column (spoiler: its a compressed XML string you can decompress and inspect, has code to do so).
Upvotes: 2