Skip to content

EntityFramework Integration

IchiSamaFR edited this page Jun 17, 2026 · 1 revision

EntityFramework Integration

MermaidSharp provides the MermaidSharp.EntityFrameworkCore package to generate entity relationship diagrams automatically from a DbContext.

It supports both:

  • Entity Framework 6 (.NET Framework 4.8)
  • Entity Framework Core (.NET 8+)

Installation

dotnet add package MermaidSharp.EntityFrameworkCore

Basic Usage

Call the ToMermaidEntityDiagram() extension method on any DbContext instance:

using MermaidSharp.EntityFrameworkCore;

var diagram = myDbContext.ToMermaidEntityDiagram();
string result = diagram.CalculateDiagram();

Customization with EntityRelationshipDiagramOptions

Pass an EntityRelationshipDiagramOptions instance to control what is included in the diagram:

var options = new EntityRelationshipDiagramOptions
{
    // Include entity columns (default: true)
    IncludeColumns = true,
    // Show PK/FK/UK annotations (default: true)
    IncludeColumnKeyTypes = true,
    // Show column comments (default: true)
    IncludeColumnComments = true,
    // Show relationships between entities (default: true)
    IncludeLinks = true,
    // Show foreign key name on links (default: true)
    IncludeLinkLabels = true,
    // Append delete behavior to link labels (default: true)
    IncludeLinkDeleteBehaviors = true,
    // Only show PK and FK columns
    FilterColumnByKeyTypes = RelationConstraintType.PrimaryKey | RelationConstraintType.ForeignKey
};
var diagram = myDbContext.ToMermaidEntityDiagram(options);
string result = diagram.CalculateDiagram();

What is Generated Automatically

Feature Description
Entities One node per non-owned entity type, ordered alphabetically
Columns Properties with their CLR type name, PK/FK/UK annotation, and optional comment
Owned entities Owned entity properties are inlined into the parent entity (EF Core only)
Relationships Foreign key links with cardinality automatically inferred from IsRequired and IsUnique
Delete behaviors Cascade, Restrict, etc. appended to the link label

Clone this wiki locally