How do you create a visual model of EntityFramework code first

前端 未结 4 1487
忘掉有多难
忘掉有多难 2020-12-07 09:19

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 co

4条回答
  •  生来不讨喜
    2020-12-07 09:45

    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.

提交回复
热议问题