$npx -y skills add managedcode/dotnet-skills --skill entity-framework-coreDesign, tune, or review EF Core data access with proper modeling, migrations, query translation, performance, and lifetime management for modern .NET applications. USE FOR: DbContext, migrations, model configuration, EF queries, tracking, loading, performance, transactions, and E
| 1 | # Entity Framework Core |
| 2 | |
| 3 | ## Trigger On |
| 4 | |
| 5 | - working on `DbContext`, migrations, model configuration, or EF queries |
| 6 | - reviewing tracking, loading, performance, or transaction behavior |
| 7 | - porting data access from EF6 or custom repositories to EF Core |
| 8 | - optimizing slow database queries |
| 9 | |
| 10 | ## Documentation |
| 11 | |
| 12 | - [EF Core Overview](https://learn.microsoft.com/en-us/ef/core/) |
| 13 | - [Performance](https://learn.microsoft.com/en-us/ef/core/performance/) |
| 14 | - [Efficient Querying](https://learn.microsoft.com/en-us/ef/core/performance/efficient-querying) |
| 15 | - [Migrations](https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/) |
| 16 | - [What's New in EF Core 9](https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-9.0/whatsnew) |
| 17 | |
| 18 | ### References |
| 19 | |
| 20 | - [patterns.md](references/patterns.md) - Query patterns, tracking strategies, loading strategies, projections, compiled queries, pagination, and temporal tables |
| 21 | - [anti-patterns.md](references/anti-patterns.md) - Common EF Core mistakes including N+1 queries, large contexts, generic repositories, and missing indexes |
| 22 | |
| 23 | ## Workflow |
| 24 | |
| 25 | 1. **Prefer EF Core for new development** unless a documented gap requires Dapper or raw SQL |
| 26 | 2. **Keep `DbContext` lifetime scoped** — align with unit of work |
| 27 | 3. **Review query translation** — check generated SQL, avoid N+1 |
| 28 | 4. **Treat migrations as first-class** — reviewable, not throwaway |
| 29 | 5. **Be deliberate about provider behavior** — cross-provider but not identical |
| 30 | 6. **Validate with query inspection** — not just in-memory mental model |
| 31 | |
| 32 | ## Current Upstream Notes |
| 33 | |
| 34 | - EF Core `v10.0.10` is a servicing release. It fixes an AOT build fork-bomb caused by EF file generation during design-time or command-line builds and an `ordinal -1 is invalid` crash when nested JSON complex sub-collections grow. Re-run AOT publishing and provider-backed JSON query/update tests when those paths apply. |
| 35 | - The July 2026 EF Core vs EF6 comparison refresh remains the first stop for migration decisions. EF Core is the active cross-platform stack, but EF6-only EDMX/ObjectContext-heavy code should not move without a feature inventory and database-backed equivalence tests. |
| 36 | |
| 37 | ## DbContext Patterns |
| 38 | |
| 39 | ### Basic Configuration |
| 40 | ```csharp |
| 41 | public class AppDbContext : DbContext |
| 42 | { |
| 43 | public DbSet<Product> Products => Set<Product>(); |
| 44 | public DbSet<Order> Orders => Set<Order>(); |
| 45 | |
| 46 | protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 47 | { |
| 48 | modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Entity Configuration (Fluent API) |
| 53 | public class ProductConfiguration : IEntityTypeConfiguration<Product> |
| 54 | { |
| 55 | public void Configure(EntityTypeBuilder<Product> builder) |
| 56 | { |
| 57 | builder.HasKey(p => p.Id); |
| 58 | builder.Property(p => p.Name).HasMaxLength(200).IsRequired(); |
| 59 | builder.HasIndex(p => p.Sku).IsUnique(); |
| 60 | builder.HasMany(p => p.OrderItems).WithOne(oi => oi.Product); |
| 61 | } |
| 62 | } |
| 63 | ``` |
| 64 | |
| 65 | ### Registration with DI |
| 66 | ```csharp |
| 67 | builder.Services.AddDbContext<AppDbContext>(options => |
| 68 | options.UseSqlServer(connectionString) |
| 69 | .EnableSensitiveDataLogging() // Dev only |
| 70 | .EnableDetailedErrors()); // Dev only |
| 71 | |
| 72 | // Or with pooling (better performance) |
| 73 | builder.Services.AddDbContextPool<AppDbContext>(options => |
| 74 | options.UseSqlServer(connectionString)); |
| 75 | ``` |
| 76 | |
| 77 | ## Query Patterns |
| 78 | |
| 79 | ### Use AsNoTracking for Read-Only |
| 80 | ```csharp |
| 81 | // Bad - tracks entities unnecessarily |
| 82 | var products = await db.Products.ToListAsync(); |
| 83 | |
| 84 | // Good - no tracking overhead |
| 85 | var products = await db.Products |
| 86 | .AsNoTracking() |
| 87 | .ToListAsync(); |
| 88 | ``` |
| 89 | |
| 90 | ### Project to DTOs |
| 91 | ```csharp |
| 92 | // Bad - loads entire entity graph |
| 93 | var orders = await db.Orders |
| 94 | .Include(o => o.Items) |
| 95 | .Include(o => o.Customer) |
| 96 | .ToListAsync(); |
| 97 | |
| 98 | // Good - loads only needed data |
| 99 | var orders = await db.Orders |
| 100 | .Select(o => new OrderDto |
| 101 | { |
| 102 | Id = o.Id, |
| 103 | CustomerName = o.Customer.Name, |
| 104 | ItemCount = o.Items.Count, |
| 105 | Total = o.Items.Sum(i => i.Price) |
| 106 | }) |
| 107 | .ToListAsync(); |
| 108 | ``` |
| 109 | |
| 110 | ### Avoid N+1 Queries |
| 111 | ```csharp |
| 112 | // Bad - N+1 problem |
| 113 | foreach (var order in orders) |
| 114 | { |
| 115 | var items = await db.OrderItems |
| 116 | .Where(i => i.OrderId == order.Id) |
| 117 | .ToListAsync(); |
| 118 | } |
| 119 | |
| 120 | // Good - eager loading |
| 121 | var orders = await db.Orders |
| 122 | .Include(o => o.Items) |
| 123 | .ToListAsync(); |
| 124 | |
| 125 | // Good - split query for large |