21

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.

Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110

3 Answers3

7

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;
        }
    }
}
redwards510
  • 1,802
  • 1
  • 17
  • 23
  • 1
    Thanks for this! Can you add your other answer as an update to this? This one is for EF Core 1.0 and the other is for 2.0 - I think it is helpful to say so. – aaron Oct 10 '17 at 10:17
  • @aaron done, I am hunting for this too! The second guy has his code for creating dgmls available on github.. – redwards510 Oct 12 '17 at 17:32
  • Two notes for the near future: At the time of this edit (4/28/2019, ~1730 EDT), the EF-Core-Power-Tools that one presently gets from VS Marketplace, version 2.2.12, does not work with VS 2019, at least not for generating the diagram. Instead use version 2.2.59 (or above I assume) from [Open VSIX gallery](http://vsixgallery.com/extension/f4c4712c-ceae-4803-8e52-0e2049d5de9f/); the [VSIX Gallery - nightly builds](https://github.com/madskristensen/VsixGalleryExtension) is itself available as an extention. Then grab DGML Editor from the VS installer. This was more effort than I expected. – R.D. Alkire Apr 28 '19 at 22:08
2

It looks like EF core doesn't have that feature currently, here is a feature comparison between EF6 and EFCore.

Kitesaint1309
  • 59
  • 2
  • 8
jjohnson8
  • 321
  • 1
  • 3
  • 12
0

Why not use Visual Studio Class Designer instead? You need to add it to your workspace with the Visual Studio Installer. On Visual Studio 2017 Installer you need to add it from the component list. See this article for more info.

Kitesaint1309
  • 59
  • 2
  • 8
  • 1
    does not have that feature the .NET Core projects – serge Jul 11 '17 at 09:58
  • 2
    @Serge No, but you can do a workaround. Create a standard .net framework console app. Add in links to your entity models from your .net core project. Now add them to the class diagram and edit. – redwards510 Oct 06 '17 at 00:53