efcore-patterns

Implements Entity Framework Core patterns for query performance, migrations, and connection resiliency.

Updated Mar 8, 2026
One-click install
npx skills add https://github.com/AGIBuild/dotnet.CI.template --skill efcore-patterns-agibuild
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: efcore-patterns
Source: https://github.com/AGIBuild/dotnet.CI.template/tree/main/.cursor/skills/efcore-patterns
Command: npx skills add https://github.com/AGIBuild/dotnet.CI.template --skill efcore-patterns-agibuild

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? EF Core projects often suffer from silent update failures, cartesian explosion in queries, unsafe migration practices, and transient database errors. This Skill provides proven patterns to configure DbContext lifetimes, optimize queries, manage migrations safely, and handle retries correctly. ## Core Features & Use Cases - Query Optimization: Apply NoTracking by default, split queries to prevent cartesian explosion, use compiled queries, and run bulk operations with ExecuteUpdateAsync/ExecuteDeleteAsync. - Migration Management: Generate, apply, and script migrations via EF Core CLI, plus run them through a dedicated migration service orchestrated with .NET Aspire. - Cross-Cutting Concerns: Implement audit timestamp and soft-delete interceptors, ExecutionStrategy retries, and DbContextFactory usage for background services and Blazor Server. - Use Case: When building an ASP.NET Core API backed by PostgreSQL, use this Skill to configure the DbContext with NoTracking defaults, add retry-on-failure, and set up a migration worker that runs before the API starts. ## Quick Start Ask the AI to set up an EF Core DbContext with NoTracking by default, retry-on-failure, and a dedicated migration service for your ASP.NET Core project.

Frequently Asked Questions about efcore-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent cartesian explosion with EF Core Include queries?▼

Use AsSplitQuery() when including two or more collection navigations. EF Core then executes separate queries per collection instead of one join producing a cartesian product. You can also set split queries globally via UseQuerySplittingBehavior in the provider options.

How do I apply EF Core migrations in production safely?▼

Generate idempotent SQL scripts with dotnet ef migrations script --idempotent, or build a migration bundle with dotnet ef migrations bundle. Avoid calling Database.Migrate() at application startup; use a dedicated migration service or deployment step instead.

Why does SaveChangesAsync do nothing after updating an entity?▼

This happens when the context uses NoTracking and the entity is not tracked, so EF Core detects no changes. Fix it by calling Update() on the entity explicitly, or query with AsTracking() before modifying it.

Can I inject DbContext into a singleton or background service?▼

No, DbContext is scoped and short-lived, so injecting it into singletons causes lifetime violations. Register and consume IDbContextFactory<T> instead, creating a new context per unit of work inside the background service.

When should I use compiled queries in EF Core?▼

Use EF.CompileAsyncQuery for queries executed thousands of times per second where query translation overhead matters. For typical CRUD operations, standard LINQ queries are sufficient and simpler to maintain.