In EF6 when you use Code First Migration, it creates a table that called __MigrationHistory
with four column:
MigrationId
ContextKey
Model
ProductVersion
What is the binary data in Model
field? Does it contain database schema?
I mean if I have a database with hundreds of tables, does the Model
field contain all of them?
What is Migrations History Table? Migrations history table is a table used by Code First Migrations to store details about migrations applied to the database. By default the name of the table in the database is __MigrationHistory and it is created when applying the first migration to the database.
Code First Migrations allow you to create a new database or update an existing database based on your model classes using the Package Manager Console for running commands. If you are using the EF code first approach then there are more ways to initialize the database provided by the Entity Framework as follows.
On MSSQL use
SELECT *, CONVERT(xml, DECOMPRESS(Model)) FROM [dbo].[__MigrationHistory]
Internally it uses this function to get value for Model field:
public virtual byte[] Compress(XDocument model)
{
DebugCheck.NotNull(model);
using (var outStream = new MemoryStream())
{
using (var gzipStream = new GZipStream(outStream, CompressionMode.Compress))
{
model.Save(gzipStream);
}
return outStream.ToArray();
}
}
So yes, it seems that the field contains whole model in a compressed form.
For the sake of EF's migration process understanding I would suggest checking Max Vasilyev's enlightening Inside of Entity Framework Migrations or How to View Generated Xml-Schema article .
The article provides a sample project. You just need to save one of the sample outputs to .edmx file e open it using Visual Studio.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With