Use when you have a written implementation plan to execute in a separate session with review checkpoints
npx skills add akires47/agent-skills --skill "dotnet-efcore-patterns"
Install specific skill from multi-skill repository
# Description
Entity Framework Core best practices including NoTracking by default, migration management, dedicated migration services, and common pitfalls to avoid.
# SKILL.md
name: dotnet-efcore-patterns
description: Entity Framework Core best practices including NoTracking by default, migration management, dedicated migration services, and common pitfalls to avoid.
Entity Framework Core Patterns
When to Use This Skill
Use this skill when:
- Setting up EF Core in a new project
- Optimizing query performance
- Managing database migrations
- Integrating EF Core with .NET Aspire
- Debugging change tracking issues
Core Principles
- NoTracking by Default - Most queries are read-only; opt-in to tracking
- Never Edit Migrations Manually - Always use CLI commands
- Dedicated Migration Service - Separate migration execution from application startup
- ExecutionStrategy for Retries - Handle transient database failures
- Explicit Updates - When NoTracking, explicitly mark entities for update
Quick Start
NoTracking by Default
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
// Disable change tracking by default for read-only queries
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
}
public DbSet<Order> Orders => Set<Order>();
}
Read-Only Queries
// Works normally with NoTracking
var orders = await _db.Orders
.Where(o => o.CustomerId == customerId)
.ToListAsync();
Updates with NoTracking
// Must explicitly enable tracking or use Entry API
var order = await _db.Orders.FindAsync(id); // FindAsync tracks automatically
order.Status = OrderStatus.Shipped;
await _db.SaveChangesAsync();
// Or use Entry API
var order = await _db.Orders.AsNoTracking().FirstAsync(o => o.Id == id);
order.Status = OrderStatus.Shipped;
_db.Entry(order).State = EntityState.Modified;
await _db.SaveChangesAsync();
Migration Management
Create Migration
dotnet ef migrations add AddOrderStatusColumn --project MyApp.Infrastructure
Apply Migrations
# Development
dotnet ef database update --project MyApp.Infrastructure
# Production - use dedicated migration service
Remove Last Migration
dotnet ef migrations remove --project MyApp.Infrastructure
References
See detailed patterns and examples in the references/ folder:
- NoTracking Pattern - Configuration, read/write operations, pitfalls
- Migration Management - CLI commands, workflow, best practices
- Migration Service - .NET Aspire integration, dedicated service pattern
- Bulk Operations - ExecuteUpdate, ExecuteDelete for performance
- Common Pitfalls - Mistakes to avoid with change tracking
- Testing Patterns - In-memory vs TestContainers
Resources
- EF Core Documentation: https://learn.microsoft.com/en-us/ef/core/
- Migrations: https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/
- Change Tracking: https://learn.microsoft.com/en-us/ef/core/change-tracking/
# Supported AI Coding Agents
This skill is compatible with the SKILL.md standard and works with all major AI coding agents:
Learn more about the SKILL.md standard and how to use these skills with your preferred AI coding agent.