AspireC4.Hosting.EntityFrameworkCore
17.0.0
dotnet add package AspireC4.Hosting.EntityFrameworkCore --version 17.0.0
NuGet\Install-Package AspireC4.Hosting.EntityFrameworkCore -Version 17.0.0
<PackageReference Include="AspireC4.Hosting.EntityFrameworkCore" Version="17.0.0" />
<PackageVersion Include="AspireC4.Hosting.EntityFrameworkCore" Version="17.0.0" />
<PackageReference Include="AspireC4.Hosting.EntityFrameworkCore" />
paket add AspireC4.Hosting.EntityFrameworkCore --version 17.0.0
#r "nuget: AspireC4.Hosting.EntityFrameworkCore, 17.0.0"
#:package AspireC4.Hosting.EntityFrameworkCore@17.0.0
#addin nuget:?package=AspireC4.Hosting.EntityFrameworkCore&version=17.0.0
#tool nuget:?package=AspireC4.Hosting.EntityFrameworkCore&version=17.0.0
AspireC4.Hosting.EntityFrameworkCore
Adds a live ER diagram of your Entity Framework Core DbContext types to an
Aspire AppHost, alongside the
AspireC4 architecture diagram and linked from it.
It is optional and additive: the architecture diagram from AddAspireC4() is always the base, and
WithEntityFrameworkDiagram() adds the ER diagram beside it. You can run the architecture diagram on its
own without this package; there is no way to have the ER diagram without it, because the extension hangs
off the builder AddAspireC4() returns.
You install it in the AppHost and call one method. Your service projects need no changes.
Prerequisites
- A
net10.0ornet11.0AppHost, built with the .NET 11 SDK. This package ships both assets. The optionalAspireC4.EntityFrameworkCoreanalyzer also needs the .NET 11 SDK in the project it is added to, and the optionalAspireC4.EntityFrameworkCore.ModelDumptool runs on the build machine's latest .NET runtime (10 or later). - An Aspire 13.6.5+ AppHost already using
AspireC4.Hosting(this package depends on it, so installing this one brings it in). - The projects that own your
DbContexttypes are registered in the AppHost withAddProject<Projects.X>(...). - Docker (or Podman) for the default viewer sidecar. See Without a container runtime if you have neither.
The requirements page has the detail behind each of these.
Container image exports verify bind sharing through Purview.Containers.Environment; preload
alpine:3.19 offline or set CONTAINER_ENVIRONMENT_PROBE_IMAGE. For WSL or shared remote
daemons, install Purview.Containers.Aspire and call builder.AddContainerEnvironment()
to adapt the viewer sidecar's mounts as well. See the
container environment guide
for mapping configuration, marker write permissions and remote-host limitations.
Enabling DbContext modelling
1. Add the package to the AppHost
dotnet add package AspireC4.Hosting.EntityFrameworkCore
2. Register the projects that own your DbContext types
The diagram is built from Aspire project resources. Every project you add with AddProject is
scanned for DbContext types, including contexts that live in class libraries the project references.
A project that is only a <ProjectReference>, or that runs as a container or executable, is not scanned.
<ItemGroup>
<ProjectReference Include="..\Orders.Api\Orders.Api.csproj" />
</ItemGroup>
3. Call WithEntityFrameworkDiagram()
var builder = DistributedApplication.CreateBuilder(args);
builder.AddAspireC4()
.WithEntityFrameworkDiagram();
builder.AddProject<Projects.Orders_Api>("orders-api");
builder.Build().Run();
Run the AppHost. The dashboard shows an aspirec4-er resource with a View ER Diagram link, and
each project that contributed a context gets an ER Diagram link. The project's element in the
architecture diagram gets one too. Output is written to ./.aspirec4/erd/ beside the AppHost.
The model is read from each project's build output (bin/), so a project must have been built
before the AppHost starts. Building the AppHost builds its referenced projects, so normally you do not
need to do anything.
4. (Optional) Increase fidelity
Without any change to your services, the model is recovered from assembly metadata. That covers
conventions and data annotations but not fluent OnModelCreating configuration, because a method
body is not in the metadata. Two opt-in packages close that gap. Each is enabled just by adding the
package reference:
| Add this package | To this project | And you get |
|---|---|---|
| (nothing) | — | Approximate: DbSet<T>, navigations, data annotations, EF naming conventions |
AspireC4.EntityFrameworkCore |
The project that declares the DbContext |
Full: adds fluent configuration (ToTable, HasKey, HasIndex, HasColumnName, …) |
AspireC4.EntityFrameworkCore.ModelDump |
The project that references the EF Core provider and has an IDesignTimeDbContextFactory<T> |
Full: the real EF IModel, with value converters and composite foreign keys |
# In the project that declares the DbContext
dotnet add package AspireC4.EntityFrameworkCore
# In the project that owns the EF Core provider and a design-time factory
dotnet add package AspireC4.EntityFrameworkCore.ModelDump
When a context is available from more than one source, the best one is used: dump, then descriptor, then metadata. Each context in the diagram records its fidelity, so an approximate model is never mistaken for a complete one.
Value objects, enums and unions
All three sources recognise Purview value objects — [Scalar<T>], [Scalar(typeof(T))],
[Scalar("Name")] and IScalarValueObject<,> as one column of the wrapped type, a bare [ValueObject] as
a complex value object flattened into its owner's table, [ValueObject(EFMapping = Json)] as one json
column — as well as shape-only value objects, EF's [ComplexType] and [Owned], collections of
primitives or value objects (one json column), enums by their underlying type, and C# unions, marked
union. Foreign keys typed with a value object resolve from either end of the relationship, including
EF's shadow foreign key when only the principal declares a collection. The full rules are in the
Value objects, enums and unions
guide.
Screenshots

The image above is captured from the sample AppHost in the
repository and stored in docs/images/screenshots/. Your own
diagram exports to ./.aspirec4/images/ on startup (ExportImages, SVG and PNG by default), and the
viewer has its own Save as SVG / PNG buttons.
Choosing which contexts appear
By default every project resource is scanned. To narrow that down:
builder.AddAspireC4()
.WithEntityFrameworkDiagram(configure: options => options
.WithProjectOptInOnly() // only projects marked below
.WithExcludedContexts("IdentityDbContext")); // short or fully-qualified name
builder.AddProject<Projects.Orders_Api>("orders-api")
.WithEntityFrameworkModel(options => options
.WithIncludedContexts("OrdersDbContext")); // per-project filter
builder.AddProject<Projects.Billing_Api>("billing-api")
.WithEntityFrameworkModel(label: "Billing"); // label the project's group
Names match case-insensitively, and an exclusion always wins over an inclusion. A context that reaches the diagram through several projects is shown once.
Renderers
Renderer |
Output | Use it for |
|---|---|---|
Interactive (default) |
.json + a browser viewer |
Exploring the schema: search, column-level edges, crow's-foot cardinality, PNG/SVG export |
Mermaid |
.mmd + preview |
Pasting into GitHub, Azure DevOps or a wiki |
Dbml |
.dbml |
dbdiagram.io and other DBML tooling |
D2 |
.d2 + a live d2 --watch sidecar |
A live d2 view |
builder.AddAspireC4()
.WithEntityFrameworkDiagram(configure: options => options.Renderer = ErdRenderer.Mermaid);
The interactive and Mermaid viewers are static files served by a small nginx sidecar, so the
dashboard has a real URL to link to. Their JavaScript ships inside this package and works offline.
Configuration
Set options in the configure callback or in the AspireC4:EntityFramework configuration section.
| Property | Default | Description |
|---|---|---|
Renderer |
Interactive |
Interactive, Mermaid, Dbml or D2 |
Title |
null |
Title shown in the diagram |
DiscoverAllProjects |
true |
Scan every project resource. WithProjectOptInOnly() restricts this to projects marked with WithEntityFrameworkModel() |
IncludedContexts / ExcludedContexts |
empty | WithIncludedContexts(...) / WithExcludedContexts(...) |
OutputDirectory |
"./.aspirec4/erd/" |
Where generated files are written, relative to the AppHost |
FileName |
"schema.gen" |
Generated file name without extension |
DomainTypeDisplay |
DomainTypeFirst |
How value objects, enums and unions appear beside the store type: AssetId (guid), ActorType (int, enum) |
GenerateGitIgnoreFile |
true |
Write a .gitignore naming the generated files. WithoutGitIgnoreFileGeneration() |
ShowNullability |
true |
Mark nullable columns with ? |
GroupBySchema / GroupByContext |
true |
Group tables by database schema, and by DbContext when there are several |
UseSubgraphs |
true |
Mermaid only: one subgraph per DbContext (Mermaid 11.17+) |
LayoutEngine |
Elk |
D2 only: Dagre, Elk or Tala |
UseViewerServer |
true |
Serve the viewer from a container. Set false where there is no container runtime |
HideFromDashboard |
false |
Hide the diagram's sidecar from the dashboard |
ExportImages |
true |
Write schema.svg / schema.png to ./.aspirec4/images/ when the model changes |
ImageFormats |
{Svg, Png} |
WithImageExport(...), or WithoutImageExport() to turn export off |
UseViewerFidelityExport |
false |
Render images through the interactive viewer in a headless browser. Pulls a ~3.7 GB Playwright image |
D2Command |
null |
A local d2 binary for image export, instead of the container |
ContainerRuntimeCommand |
"docker" |
Set to podman where Aspire uses Podman |
Set OutputDirectory in the callback rather than in configuration. The sidecar's bind mount is fixed
when the AppHost builder runs, so a value from configuration changes where files are written but not
what the sidecar serves.
Styling, layout and descriptions
Style tables, columns, types and relationships by pattern, with a separate colour for the dark theme.
Per-element callbacks can then compute a style from the model, and run on every regeneration. You can
also set the layout direction, spacing, edge style, animation and the viewer's starting theme, and
show a description for each table and column. Descriptions are off by default and come from
[Description]/[Display]/[DisplayName], the XML <summary>, or the database comment.
builder.AddAspireC4()
.WithEntityFrameworkDiagram(configure: options => options
.WithTableStyle("Order*", s => s.WithAccent("#dc2626", "#f87171").WithBadge("SALES"))
.WithTypeStyle("decimal", s => s.WithAccent("#16a34a"))
.OnTableStyle(ctx => { if (ctx.Table.Columns.Count > 15) ctx.Style.WithBadge("WIDE"); })
.WithLayout(ErdLayoutDirection.Right)
.WithDescriptions()
.WithLegend());
The interactive viewer applies all of it, and Mermaid, d2 and DBML what their formats allow. TypeScript,
Go and Python AppHosts call the same options on their withEntityFrameworkDiagram callback, apart from
the per-element callbacks and the light-and-dark palette. See
ER Diagram Styling for every
option, the per-renderer mapping and what each language can call.
Without a container runtime
// Generate the files without starting a viewer container, and render images with a local d2
builder.AddAspireC4()
.WithEntityFrameworkDiagram(configure: options =>
{
options.UseViewerServer = false; // the dashboard offers an "open locally" command instead
options.D2Command = "d2"; // or call options.WithoutImageExport()
});
// Or, with the D2 renderer, replace the d2 container with a local binary
builder.AddAspireC4()
.WithEntityFrameworkDiagram(configure: options => options.Renderer = ErdRenderer.D2)
.WithLocalD2CLI(); // must come after WithEntityFrameworkDiagram
d2 is a Go binary. Install it with scoop, Homebrew, install.sh or go install.
Troubleshooting
- No diagram content. Check the project is added with
AddProject<Projects.X>and has been built. Theaspirec4-erresource's log names each project where no model was found. - Tables are missing their fluent configuration. The context was read from metadata
(
Approximate). AddAspireC4.EntityFrameworkCoreto the project that declares it. - A composite or fluent-only relationship is not drawn. Only the model dump sees these. Add
AspireC4.EntityFrameworkCore.ModelDumpto the project with the provider and a design-time factory. - A complex or owned type configured only fluently is missing.
ComplexProperty,OwnsOneandToJsonare invisible to the static sources: mark the type[ValueObject],[ComplexType]or[Owned], or add the model dump. - Not captured from any source: inheritance mapping, delete behaviour, sequences, check constraints.
Relationships across
DbContextboundaries are never drawn.
More information
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
- Aspire.Hosting (>= 13.6.5)
- AspireC4.Hosting (>= 17.0.0)
- Purview.Containers.Environment (>= 1.0.0-prerelease.14)
- System.Reflection.MetadataLoadContext (>= 10.0.15)
-
net11.0
- Aspire.Hosting (>= 13.6.5)
- AspireC4.Hosting (>= 17.0.0)
- Purview.Containers.Environment (>= 1.0.0-prerelease.14)
- System.Reflection.MetadataLoadContext (>= 10.0.15)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 17.0.0 | 92 | 11/26/2026 |
| 17.0.0-prerelease.4 | 102 | 11/22/2026 |
| 17.0.0-prerelease.3 | 71 | 11/21/2026 |
| 17.0.0-prerelease.2 | 95 | 11/20/2026 |
| 17.0.0-prerelease.1 | 114 | 11/19/2026 |
| 13.6.5 | 390 | 11/16/2026 |
| 13.6.5-preview.1 | 92 | 11/12/2026 |
| 13.6.4-preview.3 | 78 | 11/8/2026 |
| 13.6.4-preview.2 | 114 | 10/31/2026 |
| 13.6.4-preview.1 | 88 | 10/29/2026 |
| 13.6.0-preview.4 | 76 | 10/27/2026 |
| 13.6.0-preview.3 | 95 | 10/24/2026 |
| 13.6.0-preview.2 | 70 | 10/22/2026 |
| 13.6.0-preview.1 | 117 | 10/18/2026 |