Last active
September 17, 2015 14:01
-
-
Save arman-hpp/a90804cb80aa550a2b1d to your computer and use it in GitHub Desktop.
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
using System; | |
using System.IO; | |
using System.IO.Compression; | |
using System.Linq; | |
using System.Text; | |
namespace TestMigrationHistory | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var ctx = new TestContext(); | |
var modelBase64 = ctx.Database.SqlQuery<string>(@" | |
DECLARE @binaryContent VARBINARY(MAX); | |
SELECT @binaryContent = Model FROM [__MigrationHistory]; | |
SELECT CAST('' AS XML).value( 'xs:base64Binary(sql:variable(''@binaryContent''))', 'varchar(max)' ) AS base64Content;" | |
).FirstOrDefault(); | |
if (modelBase64 == null) return; | |
var bytes = Convert.FromBase64String(modelBase64); | |
var uncompressed = Decompress(bytes); | |
var edmx = Encoding.UTF8.GetString(uncompressed); | |
File.WriteAllText(@"d:\edmx.xml", edmx); | |
} | |
static byte[] Decompress(byte[] gzip) | |
{ | |
using (var stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress)) | |
{ | |
const int size = 4096; | |
var buffer = new byte[size]; | |
using (var memory = new MemoryStream()) | |
{ | |
int count; | |
do | |
{ | |
count = stream.Read(buffer, 0, size); | |
if (count > 0) | |
{ | |
memory.Write(buffer, 0, count); | |
} | |
} | |
while (count > 0); | |
return memory.ToArray(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment