C# 如何先创建EntityFramework代码的可视化模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18658078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you create a visual model of EntityFramework code first
提问by Callum Linington
If you look hereyou will notice that this guy is showing the Entity Model Diagrams, I would like to know how I can create an Entity Model Diagram from my EntityFramework code first classes.
如果您看这里,您会注意到这个人正在展示实体模型图,我想知道如何从我的 EntityFramework 代码第一类创建实体模型图。
It just gets frustrating trying to remember how everything links together just by looking at the code.
只是通过查看代码来记住所有内容是如何链接在一起的,这只会令人沮丧。
采纳答案by Dennis Traub
With the Entity Frameworks Power Toolsinstalled you can right-click the context in your solution view, click on "Entity Framework", then select "View Entity Data Model".
随着实体框架电动工具安装在您的解决方案视图,您可以用鼠标右键单击背景下,点击“实体框架”,然后选择“查看实体数据模型”。
This will create a neat diagram from your classes.
这将从您的类中创建一个整洁的图表。
回答by Slauma
An Entity Data Model Diagram is just a visual display of an EDMX file. In order to get such a diagram from a Code-First model you must create an EDMX file from it:
实体数据模型图只是 EDMX 文件的可视化显示。为了从 Code-First 模型中获得这样的图表,您必须从中创建一个 EDMX 文件:
using System.Data.Entity.Infrastructure; // namespace for the EdmxWriter class
using (var ctx = new MyContext())
{
using (var writer = new XmlTextWriter(@"c:\Model.edmx", Encoding.Default))
{
EdmxWriter.WriteEdmx(ctx, writer);
}
}
This code creates a file Model.edmx
that you can open in Visual Studio. It will display the model diagram. The EDMX file is a snapshot of your current Code-First model. When you change the model in code you must create a new EDMX file to reflect those changes in the diagram.
此代码创建一个Model.edmx
可以在 Visual Studio 中打开的文件。它将显示模型图。EDMX 文件是您当前 Code-First 模型的快照。当您在代码中更改模型时,您必须创建一个新的 EDMX 文件以反映图表中的这些更改。
回答by Jaap
In addition to Slauma his answer. If you want to be able to adjust the layout of the diagram and you dont want to redo this every time again after creation, you can copy the Diagram node from the previously EDMX file into the new EDMX file:
除了斯劳马他的回答。如果您希望能够调整图表的布局,并且不想在创建后每次都再次重做,您可以将之前的 EDMX 文件中的 Diagram 节点复制到新的 EDMX 文件中:
string sPath = @"c:\Development\{0}";
try
{
File.Copy(String.Format(sPath, "Model.edmx"), String.Format(sPath, "ModelTemplate.edmx"));
File.Delete(String.Format(sPath, "Model.edmx"));
}
catch (Exception)
{
//no worry, file not found issues
}
using (var ctx = new ShopID.Models.ShopIDDb())
{
using (var writer = new XmlTextWriter(String.Format(sPath, "Model.edmx"), Encoding.Default))
{
EdmxWriter.WriteEdmx(ctx, writer);
}
}
XmlDocument oldModel = new XmlDocument();
oldModel.Load(String.Format(sPath, "ModelTemplate.edmx"));
XmlDocument newModel = new XmlDocument();
newModel.Load(String.Format(sPath, "Model.edmx"));
var nsmgr = new XmlNamespaceManager(newModel.NameTable);
nsmgr.AddNamespace("diagram", "http://schemas.microsoft.com/ado/2009/11/edmx");
XmlNode node = oldModel.SelectSingleNode("//diagram:Diagrams", nsmgr).ChildNodes[0];
XmlNode newNode = newModel.SelectSingleNode("//diagram:Diagrams", nsmgr);
XmlNode importNode = newNode.OwnerDocument.ImportNode(node, true);
newModel.ImportNode(importNode, true);
newNode.AppendChild(importNode);
newModel.Save(String.Format(sPath, "Model.edmx"));
File.Delete(String.Format(sPath, "ModelTemplate.edmx"));
//Updated model is ready to be opened with Visual Studio
//更新的模型已准备好使用 Visual Studio 打开
回答by Ian
To retain layout from a previous EF Power Tools generated diagram, this will carry over the Entities positions and colours etc. that exist in the new one, and leave any additions as is. Otherwise you don't see the new entities in the diagram.
为了保留之前 EF Power Tools 生成的图表的布局,这将保留新图表中存在的实体位置和颜色等,并保留任何添加内容。否则,您将看不到图中的新实体。
static void CopyLayout(string srcFile, string destFile)
{
var oldModel = XDocument.Load(srcFile);
var newModel = XDocument.Load(destFile);
XNamespace edmxNs = "http://schemas.microsoft.com/ado/2009/11/edmx";
// find all entity shapes
var oldEts = oldModel.Root.Descendants(edmxNs + "EntityTypeShape").Select(ets => ets).ToList();
var newEts = newModel.Root.Descendants(edmxNs + "EntityTypeShape").Select(ets => ets).ToList();
// replace any matching new with old
foreach (var newEt in newEts)
{
var match = oldEts.SingleOrDefault(ot => ot.Attribute(@"EntityType").Value ==
newEt.Attribute(@"EntityType").Value);
if (match != null)
newEt.ReplaceAttributes(match.Attributes());
}
newModel.Save(destFile);
}