如何从EF Core中的模型生成类图?

Sir*_*ifi 14 c# entity-framework-core

我们正在使用ASP.NET MVC Core和Entity Framework Core构建应用程序,我们在应用程序中拥有一大堆类.在以前版本的Entity Framework中,我们将使用此方法为类图生成edmx文件:

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);
     }
}
Run Code Online (Sandbox Code Playgroud)

但似乎EF Core中没有这样的功能.我想知道在Entity Framework Core中是否有相同的版本.

red*_*510 6

这家伙有什么好事!您只需添加他的 nuget 包EntityFrameworkCore.Diagrams1,它就会在您的网站中创建一个控制器 (/db-diagram/),显示您的上下文图表。有关详细信息和演示,请参阅他的网站。这仅适用于 netstandard 1.6 aka .Net Core 1.0 项目。嘘!

更新:或者,您可以将其用于 .Net Core 2.0 / EF Core 2.0 以从类创建 .Dgml 文件。这是一个小马车。使用 Visual Studio 市场或其他方式安装它。

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

这有一个选项可以添加一个扩展方法,用于从 dbcontext 文件创建 DGML。我接受了它并创建了这个控制器,在其中生成索引页面,然后在您访问 mysite.com/dgml 时为您提供 dgml 文件。和上面的想法一样。要点在这里

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


jjo*_*on8 3

目前 EF core 似乎没有该功能,这里是EF6 和 EFCore 之间的功能比较