1
---
2
name: efcore-d2-db-diagram
3
description: "Generate D2 database diagrams from Entity Framework Core models. USE FOR: EF Core database diagram, Entity Framework Core ERD, DbContext diagram, C# entity relationship diagram, PostgreSQL schema visualization, generate .d2 file from EF Core entities, Fluent API mapping diagram, migrations-based database diagram, table relationships, owned types, many-to-many join tables, indexes and constraints. DO NOT USE FOR: runtime debugging, database migration execution, schema deployment, SQL performance tuning, or draw.io diagrams."
4
---
5
6
# EF Core D2 Database Diagram Generator
7
8
## When to Use
9
10
Use this skill when the user wants to generate a database / ERD diagram from an Entity Framework Core codebase.
11
12
Typical requests:
13
14
- Generate a D2 database diagram from EF Core entities.
15
- Visualize tables, columns, primary keys, foreign keys and relationships.
16
- Analyze `DbContext`, `DbSet<T>`, `IEntityTypeConfiguration<T>`, Fluent API and migrations.
17
- Produce a `.d2` file renderable to SVG/PNG with the `d2` CLI.
18
- Document the database model of an ASP.NET Core / .NET project.
19
20
## Goal
21
22
Create a readable D2 entity-relationship diagram that reflects the actual EF Core persistence model, not only the raw C# class shape.
23
24
The diagram must prioritize:
25
26
1. Database tables and relationships.
27
2. Primary keys, foreign keys, required/optional columns.
28
3. Owned types and value objects.
29
4. Many-to-many relationships and join tables.
30
5. Indexes, unique constraints and table names.
31
6. EF Core conventions only when explicit mapping is absent.
32
33
Output is `.d2` source code. It can be rendered to SVG or PNG via the `d2` CLI.
34
35
## Tools
36
37
- **d2 CLI**: render `.d2` files to SVG/PNG.
38
- `d2 input.d2 output.svg`
39
- `d2 --layout=elk input.d2 output.svg`
40
- **d2 fmt**: format D2 files.
41
- `d2 fmt input.d2`
42
- No MCP server is required. The skill generates D2 source code as text.
43
44
## Recommended Workflow
45
46
1. Read the EF Core project structure.
47
2. Locate all `DbContext` classes.
48
3. Locate all `DbSet<T>` declarations.
49
4. Locate entity classes, owned types, enum types and value objects.
50
5. Read `OnModelCreating` and all `IEntityTypeConfiguration<T>` classes.
51
6. Read migrations when available to confirm table names, join tables, indexes and delete behaviors.
52
7. Build a normalized database model before writing D2.
53
8. Ask the mandatory diagram questionnaire before generation.
54
9. Generate the `.d2` file using the database model, not raw class nesting.
55
10. Validate D2 syntax with `d2 fmt` before delivery.
56
11. Render with `d2 --layout=elk schema.d2 schema.svg` when possible.
57
12. If regenerating, re-read EF Core mappings and migrations first.
58
59
## Mandatory Questions Before Diagram Generation
60
61
Ask these questions for every new diagram and every regeneration unless the user already answered them in the same request.
62
63
1. `Which DbContext should be diagrammed? (auto-detect/all/specific name)`
64
2. `Display columns? (all/key-only/none)`
65
3. `Display column types? (Yes/No)`
66
4. `Display nullable/required markers? (Yes/No)`
67
5. `Display indexes and unique constraints? (Yes/No)`
68
6. `Display enum values? (Yes/No)`
69
7. `Display owned types? (inline/separate/hide)`
70
8. `Display many-to-many join tables? (explicit/compact/hide)`
71
9. `Display audit/technical tables? (Yes/No)`
72
10. `Display migration-only tables not present as entities? (Yes/No)`
73
11. `Which grouping mode? (bounded-context/schema/namespace/flat)`
74
12. `Which layout engine? (elk/dagre/tala)`
75
13. `Which output format? (d2/svg/png)`
76
77
Default values, when the user asks for a quick generation:
78
79
- DbContext: `auto-detect`
80
- Columns: `key-only`
81
- Column types: `Yes`
82
- Nullable markers: `Yes`
83
- Indexes: `Yes`
84
- Enums: `No`
85
- Owned types: `inline`
86
- Join tables: `explicit`
87
- Audit/technical tables: `No`
88
- Migration-only tables: `Yes`
89
- Grouping: `bounded-context`
90
- Layout: `elk`
91
- Output: `d2`
92
93
## Reference Documents
94
95
Load these on demand when needed:
96
97
| Reference | When to load |
98
|---|---|
99
| `references/efcore-model-extraction.md` | Rules for reading DbContext, DbSet, Fluent API, configurations and migrations |
100
| `references/d2-erd-style.md` | D2 syntax and visual conventions for ERD diagrams |
101
| `references/relationship-rules.md` | How to infer one-to-one, one-to-many, many-to-many and owned relationships |
102
| `references/grouping-modes.md` | Rules for bounded-context, schema, namespace and flat grouping |
103
| `references/quality-gate.md` | Final checklist before delivering the generated diagram |
104
105
## EF Core Extraction Rules
106
107
### Source Priority
108
109
Use this priority order when sources disagree:
110
111
1. Latest applied migration / migration snapshot.
112
2. Fluent API configuration in `OnModelCreating` or `IEntityTypeConfiguration<T>`.
113
3. Data annotations.
114
4. EF Core conventions.
115
5. Raw C# class shape.
116
117
### Required EF Core Concepts to Detect
118
119
Detect and represent:
120
121
- `DbContext` and `DbSet<T>`.
122
- Entity class names and actual table names from `ToTable`.
123
- Schema names from `ToTable("Table", "schema")`.
124
- Primary keys from `HasKey`, `[Key]`, conventions and migrations.
125
- Composite keys.
126
- Foreign keys from `HasForeignKey`, navigation properties and migration operations.
127
- Delete behavior when explicit: `Cascade`, `Restrict`, `NoAction`, `SetNull`, `ClientSetNull`.
128
- Required/optional relationship markers.
129
- Owned types from `OwnsOne`, `OwnsMany` and `[Owned]`.
130
- Many-to-many relationships from `UsingEntity` and implicit EF Core join tables.
131
- Indexes from `HasIndex`, `IsUnique` and migrations.
132
- Alternate keys from `HasAlternateKey`.
133
- Shadow properties configured in Fluent API.
134
- Value conversions when they affect persisted type or readability.
135
- Enum properties.
136
- Ignored properties and ignored entities.
137
138
## Diagram Rendering Rules
139
140
### Tables
141
142
Represent each persisted table as a D2 node with `shape: sql_table` when possible.
143
144
Use this content convention:
145
146
```d2
147
Clients: {
148
shape: sql_table
149
constraint: primary_key
150
Id: uuid {constraint: primary_key}
151
Name: text
152
Status: enum
153
}
154
```
155
156
If `sql_table` is unavailable or causes validation issues, fallback to a rectangle with structured text.
157
158
### Relationships
159
160
Use directional edges from dependent table to principal table.
161
162
Labels must include relationship cardinality and FK name when known:
163
164
```d2
165
Offers.ClientId -> Clients.Id: "N:1 FK_Offers_Clients_ClientId"
166
```
167
168
Use these cardinality labels:
169
170
- `1:1`
171
- `1:N`
172
- `N:1`
173
- `N:N`
174
- `owned`
175
176
### Owned Types
177
178
Owned types default to inline rendering.
179
180
Inline example:
181
182
```d2
183
Clients: {
184
shape: sql_table
185
Id: uuid {constraint: primary_key}
186
Address.Street: text
187
Address.ZipCode: text
188
Address.City: text
189
}
190
```
191
192
If the user chooses `separate`, represent owned types as visually subordinate tables and use an `owned` relationship.
193
194
### Many-to-Many
195
196
Default to explicit join tables because EF Core creates real tables.
197
198
For implicit many-to-many relationships, create a generated join table node and mark it as `implicit join`.
199
200
### Technical Tables
201
202
Hide technical tables by default unless requested.
203
204
Examples:
205
206
- `__EFMigrationsHistory`
207
- Hangfire tables
208
- ASP.NET Identity tables
209
- Audit logs
210
- Outbox tables
211
212
If technical tables are hidden, mention them in the summary after the diagram.
213
214
## Grouping Modes
215
216
- `bounded-context`: group by detected domain area or folder/module.
217
- `schema`: group by database schema, e.g. `public`, `auth`, `billing`.
218
- `namespace`: group by C# namespace.
219
- `flat`: no containers, all tables at the same level.
220
221
## Style Rules
222
223
Use consistent styles:
224
225
- Primary entity tables: solid border.
226
- Join tables: dashed border.
227
- Owned types: lighter stroke or nested inline fields.
228
- Technical tables: muted style.
229
- External tables or migration-only tables: dotted border.
230
- Required relationships: solid line.
231
- Optional relationships: dashed line.
232
- Cascade delete: label suffix `cascade`.
233
234
## Quality Gate Before Delivery
235
236
Before delivering the diagram, verify:
237
238
- [ ] The selected DbContext is clear.
239
- [ ] All `DbSet<T>` entities are considered.
240
- [ ] Fluent API configurations are read.
241
- [ ] Migrations are checked when present.
242
- [ ] Table names and schema names match EF Core mapping.
243
- [ ] Primary keys are present.
244
- [ ] Foreign keys and cardinalities are represented.
245
- [ ] Owned types are handled according to user choice.
246
- [ ] Many-to-many join tables are explicit unless the user asked otherwise.
247
- [ ] Hidden technical tables are listed in the final summary.
248
- [ ] D2 syntax is valid with `d2 fmt`.
249
- [ ] Edge endpoints use full dot-notation when inside containers.
250
- [ ] The diagram remains readable and avoids crossing-heavy layouts.
251
252
## Output Format
253
254
When the user asks for a skill installation, provide this folder structure:
255
256
```text
257
.github/
258
skills/
259
efcore-d2-db-diagram/
260
SKILL.md
261
references/
262
efcore-model-extraction.md
263
d2-erd-style.md
264
relationship-rules.md
265
grouping-modes.md
266
quality-gate.md
267
```
268
269
When the user asks to generate a diagram, provide:
270
271
1. The `.d2` source file content.
272
2. The render command using the selected layout engine.
273
3. A concise summary of assumptions and hidden tables.
274