In Entity Framework Code-First you can enable automatic migrations based on code changes in the model.
By calling Update-Database
, these changes are:
- Registered in a
[timestamp]_AutomaticMigration
- Generate SQL scripts and migrate database
- Store the migration in the table __MigrationHistory
In the __MigrationHistory
table, the migration is described in the Model column as a large hex value. eg:
0x1F8B0800000000000400CD57CD6EDB3810BE2FB0EF20F0B40512313F976D20B5C8CAC9C2D83A09AAB4775A1ADBC492944A5281FD6C3DF491FA0A1D...
In the Package Manager Console within Visual Studio, you can retrieve a list of automatic migrations with Get-Migrations
.
However, there doesn't seem to be a way to retrieve the details of a particular automatic migration -- whether that's SQL script, or which model & fields are being affected.
Is there a way that I'm unaware of to retrieve details of a particular automatic migration?
Yes, there is.
This value in table __MigrationHistory, is a GZiped Xml, that contains the EDMX schema.
I made this following code to retrieve the EDMX model (this code uses C# 6)
As you can see, there is some syntax that is allowed in the new C# REPL that arrived with Visual Studio Update 1.
So I save this file with
.csx
extention and then I execute, in my caseDecompileMigration.csx
Then the script will put in my C:\temp folder the file emdx.xml and then I see the model in this particular migration.
To use the REPL of VS 2015 update 1, you can
Load the customized CMD called Developer Command Prompt for VS2015
Press Windows and then search for this name Developer Command Prompt for VS2015
Option 1
Open this in C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2015\Visual Studio Tools
Option 2
Open a cmd session, and then execute this following command
Option 3
Open it in Visual Studio 2015 U1 in the menu
VIEW > Other Windows > C# Interactive
If you use this option, the
Args
member in the script will not work, so you must replace theArgs[0]
for your database name, andArgs[0]
for your database name.