optimizing-ef-core-queries

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

Updated Jul 12, 2026
One-click install
npx skills add https://github.com/Patrick-Rex/DotNetTechSamples --skill optimizing-ef-core-queries-patrick-rex
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: optimizing-ef-core-queries
Source: https://github.com/Patrick-Rex/DotNetTechSamples/tree/main/.agents/plugins/dotnet-data/skills/optimizing-ef-core-queries
Command: npx skills add https://github.com/Patrick-Rex/DotNetTechSamples --skill optimizing-ef-core-queries-patrick-rex

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 query 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 Resolution: Enable SQL logging to spot repeated queries, then fix them with Include, AsSplitQuery, or projection-based loading. - Tracking Optimization: Apply AsNoTracking for read-only queries and compiled queries for hot paths to reduce overhead. - Trap Avoidance: Identify common mistakes like ToList before Where, Count instead of Any, and non-translatable string operations. - Use Case: Your API endpoint loads orders and their line items, generating hundreds of SQL statements per request. Use this Skill to rewrite the query with eager loading and projections, reducing it to one or two round trips. ## Quick Start Ask the AI to analyze my slow EF Core query that loads orders with their items and rewrite it to eliminate the N+1 problem.

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.

How to see the SQL generated by EF Core queries?

View EF Core generated SQL by calling LogTo on the DbContext options with LogLevel.Information, or by setting the Microsoft.EntityFrameworkCore.Database.Command log category to Information in appsettings.json. EnableSensitiveDataLogging shows parameter values during development.

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.

Should I use AsSplitQuery or a single query with Include?

Use a single query for one level of Include, and AsSplitQuery when including multiple collections or large child collections to avoid cartesian explosion. Single queries are preferable when transaction consistency across the loaded graph matters.

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

This happens when ToList is called before Where, causing filtering to occur in memory instead of SQL. Always apply Where and other filters before materializing results, and watch for client-evaluation warnings in the logs.

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

Use FromSqlInterpolated when LINQ cannot express a query efficiently, such as complex aggregations or vendor-specific SQL. Always use the interpolated variant rather than FromSqlRaw with string interpolation to prevent SQL injection.