optimizing-ef-core-queries
Optimize Entity Framework Core queries by fixing N+1 problems, choosing correct tracking modes, using compiled queries, and avoiding common performance traps. Use when EF Core queries are slow, generating excessive SQL, or causing high database load.
npx skills add dotnet/skills --skill optimizing-ef-core-queries --agent claude-code
Same command for any agent — swap --agent for codex, cursor, copilot.
Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.
# Optimizing EF Core Queries ## When to Use - EF Core queries are slow or generating too many SQL statements - Database CPU/IO is high due to ORM inefficiency - N+1 query patterns are detected in logs - Large result sets cause memory pressure ## When Not to Use - The user is using Dapper or raw ADO.NET (not EF Core) - The performance issue is database-side (missing indexes, bad schema) - The user is building a new data access layer from scratch ## Inputs | Input | Required | Description | |-------|----------|-------------| | Slow EF Core queries | Yes | The LINQ queries or DbContext usage to optimize | | SQL output or logs | No | EF Core generated SQL or query execution logs | ## Workflow ### Step 1: Enable query logging to see the actual SQL ```csharp // In Program.cs or DbContext configuration: optionsBuilder .UseSqlServer(connectionString) .LogTo(Console.WriteLine, LogLevel.Information) .EnableSensitiveDataLogging() // shows parameter values (dev only!) .EnableDetailedErrors(); ``` Or use the `Microsoft.EntityFrameworkCore` log category: ```json { "Logging": { "LogLevel": { "Microsoft.EntityFrameworkCore.Database.Command": "Information" } } } ``` ### Step 2: Fix N+1 query patter
- When to Use
- When Not to Use
- Inputs
- Workflow
- Step 1: Enable query logging to see the actual SQL
- Step 2: Fix N+1 query patterns
- Step 3: Use NoTracking for read-only queries
- Step 4: Use compiled queries for hot paths
- Step 5: Avoid common query traps
- Step 6: Use raw SQL or FromSql for complex queries
- Validation
- Common Pitfalls
What does the optimizing-ef-core-queries skill do?
Optimize Entity Framework Core queries by fixing N+1 problems, choosing correct tracking modes, using compiled queries, and avoiding common performance traps. Use when EF Core queries are slow, generating excessive SQL, or causing high database load.
How do I install it?
Run `npx skills add dotnet/skills --skill optimizing-ef-core-queries --agent claude-code` — it drops the skill into your project so the agent can pick it up. Swap the --agent value for codex, cursor or copilot if you use one of those.
Where does this skill come from?
From dotnet/skills, a repository with 4,927 stars. We read it straight from the repository tree rather than a submitted listing, so what you see here is what is actually published.
Is a popular skill a good skill?
Not necessarily. Stars measure attention, not adoption — a repository can trend for a week and be abandoned. That is why we show the weekly change from our own snapshots next to the total, instead of a single flattering number.
