Step 1: Install EntityFrameworkCore.Diagrams
dotnet add package EntityFrameworkCore.Diagrams
Step 2: Add middleware
Use AddEfDiagrams()
extension method in Configure()
method of your Startup
class to add middleware. Specify your DbContext
type instead of ApplicationDbContext
in the following example:
app.AddEfDiagrams<ApplicationDbContext>();
Here's how your Configure()
method might look like after this step (notice highlighted line):
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
app.AddEfDiagrams<ApplicationDbContext>();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
Notice that the middleware is added only in Development mode. This is important! Otherwise, any user in Production will se your model structure, which may not be desireable. This is not the case if you are developing public API, though.
Step 3: Start your app and see the diagram
Start your app and browse to /db-diagrams
page. You should see the diagram now.