Last active
August 1, 2017 19:54
-
-
Save AlbertoMonteiro/45198dc80641ce1896e6 to your computer and use it in GitHub Desktop.
Decompile migration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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