Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Design, 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 EF6 migration decisions. DO NOT USE FOR: unrelated stacks; generic tasks that do not need this specific guidance. INVOKES: inspect the repository context, edit targeted files, and run relevant build, test, lint, or valida
.claude/skills/managedcode-entity-framework-core/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-15 | ✗→✓ | ▲ Improved | -6% | 0% |
| case-01 | ✓→✓ | = Same ✓ | 389% | 0% |
| case-06 | ✓→✓ | = Same ✓ | 61% | 0% |
| case-02 | ✓→✓ | = Same ✓ | 96% | 0% |
| case-03 | ✓→✓ | = Same ✓ | 286% | 0% |
DbContext, migrations, model configuration, or EF queriesDbContext lifetime scoped — align with unit of workv10.0.11 is a servicing release. It fixes Azure SQL compatibility-level 170 JSON translation so nested OPENJSON projections retain AS JSON, and keeps EF compatible with .NET 11 MemoryExtensions.Min/Max overload additions. Re-run provider-backed JSON queries and compile against the repository's selected target framework after upgrading.csharppublic class AppDbContext : DbContext { public DbSet<Product> Products => Set<Product>(); public DbSet<Order> Orders => Set<Order>(); protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly); } } // Entity Configuration (Fluent API) public class ProductConfiguration : IEntityTypeConfiguration<Product> { public void Configure(EntityTypeBuilder<Product> builder) { builder.HasKey(p => p.Id); builder.Property(p => p.Name).HasMaxLength(200).IsRequired(); builder.HasIndex(p => p.Sku).IsUnique(); builder.HasMany(p => p.OrderItems).WithOne(oi => oi.Product); } }
csharpbuilder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer(connectionString) .EnableSensitiveDataLogging() // Dev only .EnableDetailedErrors()); // Dev only // Or with pooling (better performance) builder.Services.AddDbContextPool<AppDbContext>(options => options.UseSqlServer(connectionString));
csharp// Bad - tracks entities unnecessarily var products = await db.Products.ToListAsync(); // Good - no tracking overhead var products = await db.Products .AsNoTracking() .ToListAsync();
csharp// Bad - loads entire entity graph var orders = await db.Orders .Include(o => o.Items) .Include(o => o.Customer) .ToListAsync(); // Good - loads only needed data var orders = await db.Orders .Select(o => new OrderDto { Id = o.Id, CustomerName = o.Customer.Name, ItemCount = o.Items.Count, Total = o.Items.Sum(i => i.Price) }) .ToListAsync();
csharp// Bad - N+1 problem foreach (var order in orders) { var items = await db.OrderItems .Where(i => i.OrderId == order.Id) .ToListAsync(); } // Good - eager loading var orders = await db.Orders .Include(o => o.Items) .ToListAsync(); // Good - split query for large graphs var orders = await db.Orders .Include(o => o.Items) .AsSplitQuery() .ToListAsync();
csharp// Pre-compiled for frequently used queries private static readonly Func<AppDbContext, int, Task<Product?>> GetProductById = EF.CompileAsyncQuery((AppDbContext db, int id) => db.Products.FirstOrDefault(p => p.Id == id)); // Usage var product = await GetProductById(db, productId);
bash# Add migration dotnet ef migrations add AddProductIndex # Apply to database dotnet ef database update # Generate SQL script dotnet ef migrations script --idempotent -o migrate.sql
csharppublic partial class AddProductIndex : Migration { protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateIndex( name: "IX_Products_Sku", table: "Products", column: "Sku", unique: true); // Data migration (if needed) migrationBuilder.Sql(@" UPDATE Products SET NormalizedName = UPPER(Name) WHERE NormalizedName IS NULL"); } protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropIndex( name: "IX_Products_Sku", table: "Products"); } }
| Anti-Pattern | Why It's Bad | Better Approach | |--------------|--------------|-----------------| | ToList() then filter | Loads all data to memory | Filter in query | | Multiple DbContext per request | Transaction issues | Scoped lifetime | | Lazy loading everywhere | N+1 queries | Explicit Include | | Generic repository wrapper | Removes query power | Use DbContext directly | | Ignoring generated SQL | Hidden performance issues | Log and review | | SaveChanges() in loops | Many roundtrips | Batch then save |
csharp builder.HasIndex(p => p.CreatedAt); builder.HasIndex(p => new { p.Category, p.Status });
csharp var page = await db.Products .OrderBy(p => p.Id) .Skip(pageSize * pageNumber) .Take(pageSize) .ToListAsync();
csharp await db.Products .Where(p => p.Category == "Obsolete") .ExecuteDeleteAsync();
await db.Products .Where(p => p.Category == "Sale") .ExecuteUpdateAsync(p => p.SetProperty(x => x.Price, x => x.Price 0.9m));
csharp // Bad - 3 roundtrips var product = await db.Products.FindAsync(id); var reviews = await db.Reviews.Where(r => r.ProductId == id).ToListAsync(); var related = await db.Products.Where(p => p.Category == product.Category).ToListAsync();
// Good - 1 roundtrip var data = await db.Products .Where(p => p.Id == id) .Select(p => new { Product = p, Reviews = p.Reviews, Related = db.Products.Where(r => r.Category == p.Category).Take(5) }) .FirstOrDefaultAsync();
csharppublic class Product { public int Id { get; set; } public string Name { get; set; } [ConcurrencyCheck] public int Version { get; set; } // Or use RowVersion [Timestamp] public byte[] RowVersion { get; set; } } // Handle concurrency conflicts try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException ex) { var entry = ex.Entries.Single(); var databaseValues = await entry.GetDatabaseValuesAsync(); // Resolve conflict... }
Other measured skills in the registry, with their headline benchmark lift.