optimizing-ef-core-queries

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

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

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 find and fix these performance problems. ## Core Features & Use Cases - N+1 Detection and Resolution: Identify lazy-loading loops and fix them with Include, AsSplitQuery, or explicit projections. - Tracking and Compilation Tuning: Apply AsNoTracking for read-only queries and compiled queries for hot paths. - Query Trap Remediation: Fix common mistakes like ToList before Where, Count instead of Any, and non-translatable string operations. - Use Case: Your dashboard endpoint takes 8 seconds and logs show 200 SQL statements per request. Use this Skill to enable query logging, locate the N+1 pattern, and rewrite it as a single projected query. ## Quick Start Analyze my EF Core queries in the OrderRepository class and fix any N+1 problems or missing AsNoTracking calls.

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 by eager loading related data with Include, using AsSplitQuery for multiple collections to avoid cartesian explosion, or projecting only needed columns with Select. Enable EF Core query logging first to confirm how many SQL statements are generated.

How to enable EF Core SQL query logging?▼

Enable EF Core logging by calling LogTo with LogLevel.Information on the DbContext options, or set the Microsoft.EntityFrameworkCore.Database.Command category to Information in appsettings.json. EnableSensitiveDataLogging shows parameter values but should only be used in development.

When should I use AsNoTracking in EF Core?▼

Use AsNoTracking for any read-only query where you will not update the returned entities, since change tracking adds significant overhead. Use AsNoTrackingWithIdentityResolution when queries return duplicate entities to avoid duplicated objects in memory.

Should I use AsSplitQuery or a single query with Include?▼

Use a single query for one level of Include, and AsSplitQuery when you have multiple Includes or large child collections that risk cartesian explosion. Single queries are preferable when you need transaction consistency across the loaded data.

Why is my EF Core query loading the entire table into memory?▼

This happens when ToList is called before Where, or when a LINQ expression cannot be translated to SQL and falls back to client evaluation. Filter with Where before materializing, and use EF.Functions.Like instead of string.Contains for pattern matching.

When should I use raw SQL instead of LINQ in EF Core?▼

Use FromSqlInterpolated when LINQ cannot express the query efficiently, such as complex aggregations with HAVING clauses. Always use FromSqlInterpolated rather than FromSqlRaw with string interpolation to avoid SQL injection.