Skip to content

Instantly share code, notes, and snippets.

@GGOemea
Forked from redwards510/DgmlController.cs
Created March 25, 2025 21:31
Show Gist options
  • Save GGOemea/f1d9f136f6e7cc2c4687956dc6503a72 to your computer and use it in GitHub Desktop.
Save GGOemea/f1d9f136f6e7cc2c4687956dc6503a72 to your computer and use it in GitHub Desktop.
Uses EF Core Power Tools by ErikEJ (see link in code) to creates a .DGML class diagram (and serves it up for saving) of most of the EntityFrameworkCore (EF Core 2) entities in the ASP.Net MVC project when you go to yourwebsite/dgml. This is useful because there is no Class Diagram support in .Net Core 2 yet and it is difficult to find any kind o…
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 CHANGEME.Api.Controllers
{
[Route("Dgml")]
public class DgmlController : Controller
{
public CHANGEMEDbContext _context { get; }
public DgmlController( CHANGEMEDbContext context)
{
_context = context;
}
/// <summary>
/// Creates a DGML class diagram of most of the entities in the project wher you go to localhost/dgml
/// See https://github.com/ErikEJ/SqlCeToolbox/wiki/EF-Core-Power-Tools
/// </summary>
/// <returns>a DGML class diagram</returns>
[HttpGet]
public IActionResult Get()
{
System.IO.File.WriteAllText(Directory.GetCurrentDirectory() + "\\Entities.dgml",
_context.AsDgml(),
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment