How do you create a visual model of EntityFramework code first

45,665

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
45,665
Callum Linington
Author by

Callum Linington

I'm a professional software developer, C#, .NET stack and Web Development. I love rugby, play rugby, breathe rugby. Music lover, Gym lover/hater.

Updated on October 30, 2020

Comments

  • Callum Linington
    Callum Linington over 3 years

    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.

  • Callum Linington
    Callum Linington over 10 years
    That was exactly what I was looking for, couldn't quite remember the name!
  • StefanG
    StefanG about 9 years
    Simple and clean! Entity Framework Power Tools would be my first choice, but they are painful to use for complex projects.
  • Ehsan88
    Ehsan88 over 8 years
    Don't forget to run Visual Studio as administrator so that you could able to create the .edmx file
  • Efrain
    Efrain about 8 years
    Unfortunately this extension is not supported in VS2015 anymore. :(
  • Julio Nobre
    Julio Nobre about 7 years
    You can get an updated version of Entity Framework power tools 4 beta for visual studios 2010 - 2015 at GitHub (github.com/lastlink/EFPowerToolsExample). I would also suggest to Dennis Traub to consider updating his answer's link. That could prevent us from regular digging. Anyway, Dennnis, thanks a lot for poiting it out this great tool.
  • Ian
    Ian almost 7 years
    Used this for a bit and then realised that any entities/associations added to the model are not drawn in the updated one, as the corresponding diagram nodes are missing. Here's what I do now:
  • patsy2k
    patsy2k over 4 years
    how can you call this code from visual studio? where should i put it?
  • Bob.at.Indigo.Health
    Bob.at.Indigo.Health about 4 years
    Ok... I'm going to guess that the link in the answer (marketplace.visualstudio.com/…) installs something that doesn't work with the latest EF Core stuff in the latest VS 2019 (v16.4.5), because when I try to View Entity Data Model (Read-Only) it complains that it can't find a constructable class that derives from DbContext, despite the fact that the file contains public class ApplicationDbContext : IdentityDbContext {…}
  • tobbenb3
    tobbenb3 over 3 years
    Tried this, but it did not help, unfortunately: stackoverflow.com/questions/18470595/…