是的,有。
表 __MigrationHistory 中的此值是一个 GZiped Xml,其中包含 EDMX 架构。
我编写了以下代码来检索 EDMX 模型(此代码使用 C# 6)
您也可以通过以下链接访问我的 gist 上的此代码:https://gist.github.com/AlbertoMonteiro/45198dc80641ce1896e6
在这个要点中有一个 C# 5 版本,您可以将其粘贴到控制台应用程序中。
#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.dll"
#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Xml.Linq.dll"
using System.Xml.Linq;
using System.IO.Compression;
public void DecompressDatabaseMigration(string migrationName, string databaseName)
{
var connectionString = $@"Data source=.\sqlexpress;Initial catalog={databaseName};Integrated security=true";
var sqlToExecute = String.Format("select model from __MigrationHistory where migrationId like '%{0}'", migrationName);
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
var command = new SqlCommand(sqlToExecute, connection);
var reader = command.ExecuteReader();
if (!reader.HasRows)
{
throw new Exception("Now Rows to display. Probably migration name is incorrect");
}
while (reader.Read())
{
var model = (byte[])reader["model"];
var decompressed = Decompress(model);
File.WriteAllText(@"C:\temp\edmx.xml", decompressed.ToString());
}
}
}
public XDocument Decompress(byte[] bytes)
{
using (var memoryStream = new MemoryStream(bytes))
{
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
return XDocument.Load(gzipStream);
}
}
}
DecompressDatabaseMigration(Args[1], Args[0]);
如您所见,Visual Studio Update 1 附带的新 C# REPL 中允许使用某些语法。
所以我用.csx 扩展保存这个文件,然后我执行,在我的例子中是DecompileMigration.csx
csi DecompileMigration.csx
然后脚本会将文件 emdx.xml 放入我的 C:\temp 文件夹中,然后我会在此特定迁移中看到模型。
要使用VS 2015 update 1的REPL,可以
加载名为 VS2015 的开发人员命令提示符的自定义 CMD
按 Windows,然后搜索此名称 VS2015 的开发人员命令提示符
选项 1
在 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2015\Visual Studio Tools 中打开它
选项 2
打开一个 cmd 会话,然后执行以下命令
call "%vs140comntools%vsdevcmd.bat"
选项 3
在Visual Studio 2015 U1的菜单VIEW > Other Windows > C# Interactive中打开它
如果您使用此选项,则脚本中的 Args 成员将不起作用,因此您必须将 Args[0] 替换为您的数据库名称,并将 Args[0] 替换为您的数据库名称。