optimizing-ef-core-queries

Optimize Entity Framework Core LINQ queries by resolving N+1 problems and configuring tracking.

5.1k|377|Updated Feb 3, 2026
One-click install
npx skills add https://github.com/dotnet/skills --skill optimizing-ef-core-queries
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: optimizing-ef-core-queries
Source: https://github.com/dotnet/skills/tree/main/plugins/dotnet-data/skills/optimizing-ef-core-queries
Command: npx skills add https://github.com/dotnet/skills --skill optimizing-ef-core-queries

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill addresses slow or inefficient Entity Framework Core (EF Core) queries that lead to high database load and poor application performance.

Core Features & Use Cases

  • N+1 Problem Resolution: Identifies and fixes the common N+1 query pattern.
  • Query Tracking Optimization: Guides the use of AsNoTracking() for read-only scenarios.
  • Compiled Queries: Demonstrates how to use compiled queries for performance-critical paths.
  • Use Case: When your application experiences slow data retrieval and logs show excessive SQL statements, this Skill provides actionable steps to optimize EF Core queries for better performance.

Quick Start

Use the optimizing-ef-core-queries skill to optimize the provided slow EF Core query.

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 the N+1 query problem in EF Core?

To fix the N+1 query problem in EF Core, you analyze LINQ queries to identify excessive SQL generation and apply appropriate eager loading patterns. This optimization reduces database load by consolidating multiple sequential SQL statements into efficient batched queries.

When should I use AsNoTracking for EF Core query optimization?

You should use AsNoTracking for EF Core query optimization in read-only scenarios where entities are not modified. Applying this tracking behavior reduces memory overhead and improves query execution speed by skipping the context's change tracker entirely.

How do compiled queries improve EF Core performance?

Compiled queries improve EF Core performance by pre-translating LINQ expressions into SQL, bypassing repetitive query parsing overhead. This technique is effective for performance-critical paths where the same query structure executes repeatedly, yielding faster data retrieval.

Why does my EF Core query generate excessive SQL statements?

Your EF Core query generates excessive SQL statements due to inefficient LINQ projections or the N+1 query pattern causing lazy loading. Analyzing EF Core generated SQL logs helps pinpoint the exact data retrieval logic causing database resource strain.

What is the best way to tune slow Entity Framework Core queries?

The best way to tune slow Entity Framework Core queries is to analyze LINQ expressions and generated SQL logs to address N+1 problems, implement correct tracking behavior, and utilize compiled queries. This directly mitigates high database load and poor application performance.