Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Last active August 1, 2017 19:54
Show Gist options
  • Save AlbertoMonteiro/45198dc80641ce1896e6 to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/45198dc80641ce1896e6 to your computer and use it in GitHub Desktop.
Decompile migration
#r "System.Xml"
#r "System.Xml.Linq"
#r "System.IO.Compression"
#r "System.Data"
using System.Xml.Linq;
using System.IO.Compression;
using System.Data.SqlClient;
public void DecompressDatabaseMigration(string migrationName, string databaseName)
{
var connectionString = $@"Data source=.\sqlexpress;Initial catalog={databaseName};Integrated security=true";
var sqlToExecute = $"select model from __MigrationHistory where migrationId like '%{migrationName}'";
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand(sqlToExecute, connection))
{
connection.Open();
var model = (byte[])command.ExecuteScalar();
var decompressed = Decompress(model);
File.WriteAllText(@"C:\temp\edmx.xml", decompressed.ToString());
}
}
public XDocument Decompress(byte[] bytes)
{
using (var gzipStream = new GZipStream(new MemoryStream(bytes), CompressionMode.Decompress, false))
return XDocument.Load(gzipStream);
}
DecompressDatabaseMigration(Args[1], Args[0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment