ef-core

Provide EF Core guidance for DbContext lifetime, query performance, migrations, transactions, and concurrency.

Updated Mar 1, 2026
One-click install
npx skills add https://github.com/CloudyWing/ai-dotfiles --skill ef-core-cloudywing
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ef-core
Source: https://github.com/CloudyWing/ai-dotfiles/tree/main/skills/ef-core
Command: npx skills add https://github.com/CloudyWing/ai-dotfiles --skill ef-core-cloudywing

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Prevents common Entity Framework Core mistakes that cause performance regressions, concurrency conflicts, incorrect migrations, and lifetime-related runtime bugs by prescribing clear, production-ready patterns for DbContext usage, querying, transactions, and migrations.

Core Features & Use Cases

  • DbContext lifetime guidance: Enforce Scoped registration, avoid injecting DbContext into singletons, and recommend IDbContextFactory for background services.
  • Query performance: Encourage AsNoTracking for read-only queries, projection via Select to avoid loading full entities, and strategies to prevent N+1 problems.
  • Migration & change management: Require migration files in version control, discourage editing applied migrations, use descriptive names, and prefer HasData for seeding.
  • Transactions and concurrency: Recommend appropriate transaction patterns, single SaveChangesAsync per unit of work, and optimistic concurrency controls with clear handling strategies.

Quick Start

Request an audit of your .NET project for Microsoft.EntityFrameworkCore usage and generate actionable fixes for DbContext lifetime, AsNoTracking/projection, migration naming, transaction scope, and concurrency handling.

Frequently Asked Questions about ef-core

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

FAQPage Schema
How do I prevent N+1 queries in EF Core?

Prevent EF Core N+1 queries by applying AsNoTracking for read-only data, using Select projections to load only required fields, and structuring queries to fetch related data in a single trip.

What is the correct DbContext lifetime for background services?

The correct DbContext lifetime for background services uses IDbContextFactory instead of direct injection, because standard Scoped registration causes runtime bugs when accessed from Singleton services.

How do I manage EF Core migrations in version control?

Manage EF Core migrations in version control by committing migration files, using descriptive naming, avoiding edits to already applied migrations, and utilizing HasData for database seeding.

How do I handle optimistic concurrency in Entity Framework Core?

Handle optimistic concurrency in Entity Framework Core by implementing single SaveChangesAsync per unit of work, applying appropriate transaction patterns, and defining clear conflict resolution strategies.

When should I use AsNoTracking in EF Core queries?

Use AsNoTracking in EF Core queries for read-only operations where entities do not require change tracking, significantly improving query performance by bypassing the change tracker overhead.

Does injecting a Scoped DbContext into a Singleton cause issues?

Injecting a Scoped DbContext into a Singleton causes concurrency and lifetime issues, requiring IDbContextFactory to generate DbContext instances safely within background service execution.