efcore-patterns
Agent skill fromAaronontheweb/dotnet-skills
Entity Framework Core best practices including NoTracking by default, query splitting for navigation collections, migration management, dedicated migration services, and common pitfalls to avoid.
How to install efcore-patterns
install efcore-patterns
# install the dotnet-skills pack with the skills CLI
npx skills add Aaronontheweb/dotnet-skills# or add only this skill to Claude Code: copy the folder
skills/efcore-patterns/
# into
~/.claude/skills/efcore-patterns/ (all projects)
.claude/skills/efcore-patterns/ (one project)
1# Entity Framework Core Patterns23## When to Use This Skill45Use this skill when:6- Setting up EF Core in a new project7- Optimizing query performance8- Managing database migrations9- Integrating EF Core with .NET Aspire10- Debugging change tracking issues11- Loading multiple navigation collections efficiently (query splitting)1213## Core Principles14151. **NoTracking by Default** - Most queries are read-only; opt-in to tracking162. **Never Edit Migrations Manually** - Always use CLI commands173. **Dedicated Migration Service** - Separate migration execution from application startup184. **ExecutionStrategy for Retries** - Handle transient database failures195. **Explicit Updates** - When NoTracking, explicitly mark entities for update2021---2223## Pattern 1: NoTracking by Default2425Configure your DbContext to disable change tracking by default. This improves performance for read-heavy workloads.2627```csharp28public class ApplicationDbContext : DbContext29{30 public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)31 : base(options)32 {33 // Disable change tracking by default for better performance on read-only queries34 // Use .AsTracking() explicitly for queries that need to track changes35 ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;36 }3738 public DbSet<Order> Orders => Set<Order>();39 public DbSet<Customer> Customers => Set<Customer>();40}41```4243### When NoTracking is Active4445**Read-only queries work normally:**46…
Excerpt shown under the repository's MIT license.
