我可以从指定的迁移中解码EntityFramework模型吗? [英] Can I get decode an EntityFramework Model from a specified migration?
问题描述
显然,IMigrationMetadata.Target对EF模型的状态进行编码。
我可以用它来为特定的迁移重建模型吗?
Apparently IMigrationMetadata.Target encodes the state of the EF model. Can I use this to reconstruct the model for a particular migration?
推荐答案
是的,有可能。我自己很好奇那些魔术资源字符串到底存储了什么。通过深入研究实体框架源代码(请参见 DbMigrator.GetLastModel()
方法),我发现 IMigrationMetadata.Target
仅存储包含gzip压缩XML的base-64字符串数据。为了测试这一点,我创建了一个新的控制台应用程序,其中包含一个简单的代码优先模型,定义如下:
Yes, it is possible. I was myself curious what exactly those magic resource strings were storing. By digging into the Entity Framework source (see the DbMigrator.GetLastModel()
method), I found out that the IMigrationMetadata.Target
just stores a base-64 string containing gzipped XML data. To test this, I created a new console application containing a simple code-first model defined as follows:
public class ContactContext : DbContext
{
public virtual IDbSet<Contact> Contacts { get; set; }
}
public class Contact
{
public int Id {get; set;}
public string FirstName { get; set; }
public string LastName { get; set; }
}
然后,我使用NuGet软件包管理器控制台创建了迁移:
Then I created a migration using the NuGet Package Manager Console:
PM> Enable-Migrations
PM> Add-Migration MyMigration
接下来,我将以下代码添加到应用程序的 Main( )
方法来解码该字符串中的值并将其转储到控制台:
Next I added the following code to my application's Main()
method to decode the value in that string and dump it to the console:
var migration = new MyMigration();
var metadata = (IMigrationMetadata)migration;
var compressedBytes = Convert.FromBase64String(metadata.Target);
var memoryStream = new MemoryStream(compressedBytes);
var gzip = new GZipStream(memoryStream, CompressionMode.Decompress);
var reader = new StreamReader(gzip);
Console.WriteLine(reader.ReadToEnd());
此输出一个EDMX文件,该文件表示与创建迁移的我 DbContext
关联的实体数据模型。如果我将此输出写入扩展名为 .edmx
的文件中,则可以使用Visual Studio将其打开并在实体设计器中查看。
This outputs an EDMX file representing the Entity Data Model associated with my DbContext
that created the migration. If I write this output to a file with the .edmx
extension, I'm able to open it with Visual Studio and view it in the Entity Designer.
然后,如果由于某种原因我想重新生成 DbContext
和生成模型的实体类,则需要仅执行以下操作:
Then if for some reason I wanted to regenerate the DbContext
and entity classes that produced the model, I would need only do the following:
- 将
.edmx
文件添加到Visual Studio项目中。 - 安装用于C#的EF 5.x DbContext Generator (如果我还没有的话)。
- 通过选择
添加->添加相关的T4模板。
- 修改新添加的
.tt
文件,替换$ edmxInputFile $
,其文件名为我的.edmx
文件。 - 观看两个模板神奇地将我的代码优先类型重新生成为各自的
.cs
文件。
- Add the
.edmx
file to a Visual Studio project. - Install the EF 5.x DbContext Generator for C# if I don't already have it.
- Add the related T4 templates by selecting
Add -> New Item
from project node context menu. - Modify the newly added
.tt
files, replacing$edmxInputFile$
with the name of my.edmx
file. - Watch as the two templates magically regenerate my code-first types to their respective
.cs
files.
<希望能回答您的问题! :-D
Hope that answers your question! :-D
这篇关于我可以从指定的迁移中解码EntityFramework模型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!