Skip to main content
 首页 » 编程设计

c#之如何描述自动迁移的细节

2024年02月05日11mayingbao

在 Entity Framework Code-First 中,您可以根据模型中的代码更改启用自动迁移。

通过调用 Update-Database,这些更改是:

  1. 在 [timestamp]_AutomaticMigration 中注册
  2. 生成SQL脚本并迁移数据库
  3. 将迁移存储在表 __MigrationHistory 中

在 __MigrationHistory 表中,模型列中的迁移被描述为一个大的十六进制值。例如:

0x1F8B0800000000000400CD57CD6EDB3810BE2FB0EF20F0B40512313F976D20B5C8CAC9C2D83A09AAB4775A1ADBC492944A5281FD6C3DF491FA0A1D... 

在 Visual Studio 的包管理器控制台中,您可以使用 Get-Migrations 检索自动迁移列表。

但是,似乎没有办法检索特定自动迁移的详细信息——无论是 SQL 脚本,还是受影响的模型和字段。

是否有一种我不知道的方法可以检索特定自动迁移的详细信息?

请您参考如下方法:

是的,有。

表 __MigrationHistory 中的这个值是一个 GZiped Xml,其中包含 EDMX 架构。

我编写了以下代码来检索 EDMX 模型(此代码使用 C# 6)

You can access this code on my gist also, with this link: https://gist.github.com/AlbertoMonteiro/45198dc80641ce1896e6

In this gist there is a C# 5 version, that you can paste in a console application.

#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 session ,然后执行以下命令

call "%vs140comntools%vsdevcmd.bat" 

选项 3

在 Visual Studio 2015 U1 的菜单 VIEW > Other Windows > C# Interactive 中打开它

如果您使用此选项,脚本中的Args 成员将不起作用,因此您必须将Args[0] 替换为您的数据库名称,并将Args[0] 为您的数据库名称。