Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is model column in MigrationHistory table?

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?

like image 898
Nasser Sadraee Avatar asked Aug 26 '14 08:08

Nasser Sadraee


People also ask

What is __ MigrationHistory table?

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.

What are Code First Migrations?

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.


3 Answers

On MSSQL use

SELECT *, CONVERT(xml, DECOMPRESS(Model)) FROM [dbo].[__MigrationHistory]
like image 57
Milan Avatar answered Oct 11 '22 14:10

Milan


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.

like image 20
SoftwareFactor Avatar answered Oct 11 '22 13:10

SoftwareFactor


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.

like image 35
Julio Nobre Avatar answered Oct 11 '22 15:10

Julio Nobre