Designs and diagnoses Entity Framework Core queries, tracking, model configuration, DbContext lifetimes, transactions, concurrency, and migrations. Use when EF Core behavior is central; do not use for non-EF data access (use performance-review or csharp-best-practices) or broad API review.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Efcore Patterns?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/k4ndev-efcore-patterns)
---
name: efcore-patterns
description: Designs and diagnoses Entity Framework Core queries, tracking, model configuration, DbContext lifetimes, transactions, concurrency, and migrations. Use when EF Core behavior is central; do not use for non-EF data access (use performance-review or csharp-best-practices) or broad API review.
---
# EF Core Patterns
## Purpose
Make EF Core persistence correct and efficient without assuming provider behavior or confusing test doubles with relational semantics.
## Scope
LINQ-to-Entities, tracking, entity configuration, `DbContext`, transactions, optimistic concurrency, query plans, and EF migrations.
## When to use
- A task references EF Core queries, `DbContext`, `SaveChanges`, migrations, provider translation, or tracking.
- A performance question involves an EF query; coordinate with `performance-review` when measurement is needed.
## Do not use for
- Dapper/ADO.NET-only code: use `performance-review` / `csharp-best-practices` as appropriate.
- Entire HTTP endpoint contracts: use `api-contract-review` or `dotnet-code-review` when review intent exists.
## Required context
Identify EF Core and provider versions, database engine, context lifetime/registration, entity configuration, migrations applied in each environment, query workload/data size, and test provider.
## Workflow
1. Inspect LINQ expression and generated SQL for nontrivial translation; confirm server-side filtering/projection and inspect query plan when performance is the concern.
2. Project only required fields; use no-tracking for read-only paths when identity resolution/change tracking are not needed.
3. Bound result sets and use deterministic keyset/offset pagination appropriate to ordering and product requirements.
4. Check N+1, cartesian expansion, split-query trade-offs, accidental client-side work, repeated enumeration, and parameterization.
5. Keep `DbContext` scoped to a unit of work; never run parallel operations on the same context instance.
6. Define transaction boundaries for the full business operation. Use concurrency tokens when lost updates matter; inspect retry execution strategy interactions with manual transactions.
7. Treat migration SQL as provider- and deployment-specific: inspect locks, defaults, data backfill, rollback, and expand/contract compatibility.
8. Never edit an applied migration or execute a production migration without explicit authorization. Test relational semantics with the real provider or a compatible relational test database; EF InMemory is not a relational substitute.
## Decision rules
- Do not add `Include` for fields that projection can serve; do not remove tracking where updates or identity semantics depend on it.
- Performance claims require SQL/plan/measurement evidence; correctness findings may be explained from verified semantics.
- For multi-resource atomic writes, an EF transaction alone cannot make a broker/API side effect atomic; use an outbox design when required.
## Expected output format
Describe provider/version assumptions, query or unit-of-work behavior, corrected code/SQL shape, operational migration risks, and targeted relational tests. State when SQL or plan was not inspected.
## Common mistakes
- Assuming `AsNoTracking` is always faster, using an unbounded query, sharing a context across tasks, relying on InMemory for relational behavior, or hand-editing deployed migrations.
## Edge cases
- Global query filters may affect admin/soft-delete behavior; verify intentional bypasses.
- `ExecuteUpdate`/`ExecuteDelete` bypass change tracking and may bypass domain hooks; verify target EF version and invariants.
- Retrying transactions can repeat delegate side effects; keep retried work idempotent and database-contained.
## Examples
See [EF Core examples](examples.md) for bounded projection and concurrency-safe updates.
## Test scenarios
- Positive: “Diagnose this EF Core list endpoint's N+1 and propose an integration test using SQL Server.”
- Negative: “What is C# nullable reference type syntax?” routes to `csharp-best-practices`.
- Boundary: query speed issue selects this skill and `performance-review`; a DB-free LINQ-to-Objects question does not.
## Related skills
`performance-review`, `dotnet-code-review`, `api-contract-review`, `messaging-patterns`, `testing-quality`, `security-audit`.