How do you create a visual model of EntityFramework code first

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