optimizing-ef-core-queries

Diagnose and optimize slow Entity Framework Core queries by fixing N+1 patterns and tracking overhead.

1|Updated Jun 1, 2026
One-click install
npx skills add https://github.com/D1ssolve/craft-agents --skill optimizing-ef-core-queries-d1ssolve
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: optimizing-ef-core-queries
Source: https://github.com/D1ssolve/craft-agents/tree/main/skills/optimizing-ef-core-queries
Command: npx skills add https://github.com/D1ssolve/craft-agents --skill optimizing-ef-core-queries-d1ssolve

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? EF Core applications often suffer from slow queries, excessive SQL statements, and high database load caused by N+1 patterns, unnecessary change tracking, and client-side evaluation. This Skill provides a systematic workflow to identify and fix these performance problems. ## Core Features & Use Cases - N+1 Detection and Fixes: Enable SQL logging, then resolve N+1 patterns using Include, AsSplitQuery, or projection-based queries. - Tracking and Compilation Optimization: Apply AsNoTracking for read-only queries and EF.CompileAsyncQuery for hot paths. - Common Trap Remediation: Fix issues like ToList-before-Where, Count instead of Any, and unsafe FromSqlRaw interpolation. - Use Case: Your API endpoint executes 50 SQL statements per request due to lazy loading in a loop. Use this Skill to rewrite it with eager loading and projections, reducing it to a single query. ## Quick Start Analyze my EF Core queries in OrderService.cs and fix the N+1 problems and tracking overhead causing slow response times.

Frequently Asked Questions about optimizing-ef-core-queries

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

FAQPage Schema
How do I fix N+1 queries in Entity Framework Core?

Fix N+1 queries in EF Core by eager loading related data with Include, using AsSplitQuery for multiple collections to avoid cartesian explosion, or projecting only needed columns with Select. Enable SQL logging first to confirm the N+1 pattern exists.

When should I use AsNoTracking in EF Core?

Use AsNoTracking for read-only queries where entities will not be updated, since change tracking adds significant overhead. Use AsNoTrackingWithIdentityResolution when queries return duplicate entities to avoid duplicated objects in memory.

What is the difference between AsSplitQuery and a single Include query?

A single Include query joins everything in one SQL statement, which risks cartesian explosion with multiple collections. AsSplitQuery issues separate SQL statements per collection, which is better for multiple Includes or large child collections.

Does this apply to Dapper or raw ADO.NET applications?

No, this guidance is specific to Entity Framework Core. If you use Dapper or raw ADO.NET, or if the bottleneck is database-side like missing indexes or schema design, these EF Core techniques do not apply.

Why does my EF Core query load the entire table into memory?

This happens when ToList is called before Where, causing the full table to load before filtering in memory. Always apply Where filters before materializing with ToList so filtering happens in SQL.

How do I perform bulk updates in EF Core without fetching entities?

In EF Core 7 and later, use ExecuteUpdateAsync and ExecuteDeleteAsync to run bulk operations directly in SQL without loading entities into memory. This avoids the fetch-then-save pattern that causes unnecessary round trips.