If you look here you 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.
It just gets frustrating trying to remember how everything links together just by looking at the code.
With the Entity Frameworks Power Tools installed 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.
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:
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.
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:
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
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.
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);
}