efcore-patterns

Implements Entity Framework Core patterns for tracking, migrations, query splitting, and bulk operations.

Updated Aug 10, 2025
One-click install
npx skills add https://github.com/afonsoft/ai-studio-workspace --skill efcore-patterns-afonsoft
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: efcore-patterns
Source: https://github.com/afonsoft/ai-studio-workspace/tree/main/agents/skills/efcore-patterns
Command: npx skills add https://github.com/afonsoft/ai-studio-workspace --skill efcore-patterns-afonsoft

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? EF Core misconfiguration causes silent update failures, N+1 queries, cartesian explosion, and unsafe migration handling. This Skill provides proven patterns to configure DbContext correctly, manage migrations safely, and optimize query performance in .NET applications. ## Core Features & Use Cases - NoTracking by Default: Configure read-optimized DbContext with explicit Update() calls for writes, avoiding silent SaveChanges failures. - Migration Management: Use CLI commands exclusively, run migrations through a dedicated Aspire migration service with ExecutionStrategy retries. - Query Splitting & Bulk Operations: Prevent cartesian explosion with SplitQuery and use ExecuteUpdate/ExecuteDelete for efficient bulk changes. - Use Case: When building an ASP.NET Core API with PostgreSQL and Aspire, apply these patterns to set up the DbContext, run migrations before app startup, and avoid common performance pitfalls. ## Quick Start Apply the efcore-patterns skill to configure my ApplicationDbContext with NoTracking by default and set up a dedicated migration service for my Aspire project.

Frequently Asked Questions about efcore-patterns

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

FAQPage Schema
How do I configure EF Core NoTracking by default?▼

Set ChangeTracker.QueryTrackingBehavior to QueryTrackingBehavior.NoTracking in your DbContext constructor. Read queries then run without tracking overhead, but updates require calling Update() explicitly or using AsTracking() on the query.

How to run EF Core migrations in a .NET Aspire application?▼

Create a dedicated migration service project with a BackgroundService that calls Database.MigrateAsync() inside an ExecutionStrategy. In the AppHost, use WaitForCompletion on the migration project so the API starts only after migrations finish.

Why does SaveChanges not update my entity in EF Core?▼

This happens when the DbContext uses NoTracking and the entity was loaded without tracking, so changes are never detected. Fix it by calling Update(entity) before SaveChangesAsync, or by adding AsTracking() to the query that loaded the entity.

What is query splitting in EF Core and when should I use it?▼

Query splitting loads each included navigation collection in a separate SQL query, preventing cartesian explosion when joining multiple collections. Enable SplitQuery globally and override with AsSingleQuery() for small, well-understood graphs where one round-trip is cheaper.

Can I use ExecuteUpdate and ExecuteDelete for bulk operations in EF Core?▼

Yes, EF Core 7+ provides ExecuteUpdateAsync and ExecuteDeleteAsync, which translate directly to single SQL UPDATE or DELETE statements without loading entities into memory. They are ideal for batch status changes or deleting old records.

Should I edit EF Core migration files manually?▼

No, never edit, rename, or delete migration files directly. Always use dotnet ef migrations add and dotnet ef migrations remove so the model snapshot stays consistent; the only exception is adding custom SQL inside the Up() or Down() methods.