optimizing-ef-core-queries

Featured

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.

API & Backend 463 stars 35 forks Updated 2 days ago MIT

Install

View on GitHub

Quality Score: 94/100

Stars 20%
89
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# 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 patterns **The #1 EF Core performance killer.** Happens when loading related entities in a loop. **Before (N+1 — 1 query for orders + N queries for items):** ```csharp var orders = await db.Orders.ToListAsync(); foreach (var order in orders) { // Each a...

Details

Author
managedcode
Repository
managedcode/dotnet-skills
Created
4 months ago
Last Updated
2 days ago
Language
C#
License
MIT

Similar Skills

Semantically similar based on skill content — not just same category

Data & Documents Featured

entity-framework-core

Design, tune, or review EF Core data access with proper modeling, migrations, query translation, performance, and lifetime management for modern .NET applications. USE FOR: DbContext, migrations, model configuration, EF queries, tracking, loading, performance, transactions, and EF6 migration decisions. DO NOT USE FOR: unrelated stacks; generic tasks that do not need this specific guidance. INVOKES: inspect the repository context, edit targeted files, and run relevant build, test, lint, or validation commands when changes are made.

463 Updated 2 days ago
managedcode
Code & Development Listed

ef-core-query-review

Review EF Core LINQ queries for N+1, cartesian explosion, tracking overhead, client-side evaluation, and over-fetching. Use when writing or reviewing any code that queries a DbContext.

0 Updated today
Sarmkadan
API & Backend Listed

n-plus-one-hunter

Find and fix N+1 queries and related ORM query anti-patterns — the loop that quietly fires one database query per row. Reviews application code for N+1s, nested N+1s, count/exists-in-a-loop, cartesian eager-load blowups, over-fetching, and missing foreign-key indexes, then rewrites them with the correct eager-load strategy and shows the query-count before/after. Use whenever writing or reviewing code that loads a collection and touches an association, a serializer or API response, a GraphQL resolver, a view template, or a background job — in ActiveRecord/Rails, the Django ORM, Prisma, SQLAlchemy, Sequelize, TypeORM, Ecto, or raw query code — and especially before merging an endpoint that renders a list.

0 Updated 6 days ago
windchillscalanthes-ship-it