How to generate class diagram from models in EF Core?

c# entity-framework-core

Question

We are building an application using ASP.NET MVC Core and Entity Framework Core and we have the whole bunch of classes in our application. In previous versions of Entity Framework, we would use this method for generating an edmx file for class diagram:

void ExportMappings(DbContext context, string edmxFile)
{
     var settings = new XmlWriterSettings { Indent = true };
     using (XmlWriter writer = XmlWriter.Create(edmxFile, settings))
     {
         System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(context, writer);
     }
}

but it seems that there's no such feature in EF Core. I wonder if there's an equivalent version for doing this in Entity Framework Core.

1
14
12/4/2016 7:10:44 AM

Popular Answer

This guy is onto something good! You just add his nuget package EntityFrameworkCore.Diagrams1 and it creates a controller (/db-diagram/) in your website that displays a diagram of your context. See his site for details and a demo. This is only for netstandard 1.6 aka .Net Core 1.0 projects. Boo!

Update: Alternatively you can use this for .Net Core 2.0 / EF Core 2.0 to create .Dgml files from the classes. It is a little buggy. Install it using Visual Studio marketplace or whatever.

https://github.com/ErikEJ/SqlCeToolbox/wiki/EF-Core-Power-Tools

This has an option to add an extension method for creating a DGML from your dbcontext file. I took that and created this controller where the index page generates and then serves you the dgml file when you go to mysite.com/dgml. The same idea as the one above. gist here

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace OL.Web.AffiliateDb.Api.Controllers
{   
    [Route("Dgml")]
    public class DgmlController : Controller
    {
        public SomeDbContext _context { get; }


        public DgmlController( SomeDbContext context)
        {            
          _context = context;                       
        }

        /// <summary>
        /// Creates a DGML class diagram of most of the entities in the project wher you go to localhost/dgml
        /// </summary>
        /// <returns>a DGML class diagram</returns>
        [HttpGet]
        public IActionResult Get()
        {

            System.IO.File.WriteAllText(Directory.GetCurrentDirectory() + "\\Entities.dgml",
                _context.AsDgml(), // https://github.com/ErikEJ/SqlCeToolbox/wiki/EF-Core-Power-Tools
                System.Text.Encoding.UTF8);

            var file = System.IO.File.OpenRead(Directory.GetCurrentDirectory() + "\\Entities.dgml");
            var response = File(file, "application/octet-stream", "Entities.dgml");
            return response;
        }
    }
}
4
10/12/2017 5:29:12 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow