# optimizing-ef-core-queries

Reviews EF Core data access in services, repositories, imports and background jobs, and points at queries that run slowly, duplicate rows or hit the database too often.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode · 5700 installs · ★ 5730
- Author: dotnet
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/dotnet/skills/tree/HEAD/plugins/dotnet-data/skills/optimizing-ef-core-queries/SKILL.md
- Updated: 2026-10-24
- HTML: https://affpapa.org/en/nevibecode/skills/optimizing-ef-core-queries

## What it does

The skill reads data access code built on Entity Framework Core: services, repositories, imports, background jobs. It hunts for queries that are slow or wrong — extra round-trips, N+1 from lazy loading, repeated `Include` calls that duplicate rows, deep pages behind `Skip`, `SaveChanges` buried in a chain of calls.

The workflow is ordered. It locates the EF boundary first (`DbContext`, `DbSet`, the repository implementation, model-building code), then traces each query to execution or enumeration. A separate check covers what the operators bind to: `Queryable` or `Enumerable`. The second kind can hide a query that re-runs on every enumeration. Findings come with a location, the affected call path, and a remedy that keeps result order, paging and transaction behaviour intact.

Confirm the output against SQL before trusting it. The skill says as much itself: source reasoning is not a measured plan, and a missing `HasIndex` in one supplied file does not prove the deployed database lacks an index. So it suggests turning on `LogTo` or the `Microsoft.EntityFrameworkCore.Database.Command` level in `appsettings.json`, and tagging the query with `.TagWith`. Pointed at Dapper or raw ADO.NET, it sticks to SQL and leaves the EF APIs alone.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add dotnet/skills --skill optimizing-ef-core-queries -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/optimizing-ef-core-queries/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/optimizing-ef-core-queries/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add dotnet/skills --skill optimizing-ef-core-queries -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/optimizing-ef-core-queries/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add dotnet/skills --skill optimizing-ef-core-queries -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/optimizing-ef-core-queries/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install dotnet/skills optimizing-ef-core-queries
```

2. **Manually: put the files in place**
- File: `.github/skills/optimizing-ef-core-queries/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add dotnet/skills --skill optimizing-ef-core-queries -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/dotnet/skills --path plugins/dotnet-data/skills/optimizing-ef-core-queries
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/optimizing-ef-core-queries/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add dotnet/skills --skill optimizing-ef-core-queries -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/dotnet/skills/tree/HEAD/plugins/dotnet-data/skills/optimizing-ef-core-queries/SKILL.md

## When to use it
- find N+1 queries in a repository before release
- cut the number of SQL statements a background job fires
- explain why deep pages slow down as `Skip` grows
- check whether a filter runs in memory instead of SQL
- see if a filter and sort have an index behind them

## Example prompts
- Review `OrderRepository.cs` for EF Core issues — why does loading orders fire so many queries?
- Audit `Jobs/ImportJob.cs`: any hidden `SaveChanges` or premature materialization there?
- Look at the paging in `ProductsService.cs`. Why do the later pages take longer?

## Source text · SKILL.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: fb62f4ac438ebbc7.

BEGIN THIRD-PARTY FILE fb62f4ac438ebbc7
```text
---
name: optimizing-ef-core-queries
description: "Review and optimize application data access that uses Entity Framework Core (EF Core). Use for EF Core performance and correctness reviews of services, repositories, imports, and background jobs: slow or memory-heavy queries, excessive round-trips, hidden SaveChanges, premature materialization, dynamic query construction, result-graph tracking, bulk writes, DbContext lifetime or overlapping operations, search semantics, paging, raw SQL, and model/index coverage. Use whether or not EF Core owns the schema. For EF Core, not Dapper or raw ADO.NET."
license: MIT
---

# Optimizing EF Core Queries

Diagnose and fix slow or incorrect application data access built on EF Core. In a source-only review, trace the code without executing it and separate proven code behavior from runtime assumptions. When SQL, logs, or plans are available, use them to confirm the bottleneck. Prefer changes that reduce round-trips, duplicated rows, scans, or per-call translation cost over micro-optimizations.

## When to Use

- EF Core queries are slow or emit far more SQL statements than expected
- The same query repeats once per row (N+1 / lazy loading)
- Multiple collection `Include`s blow up or duplicate rows
- Deep pages slow down as `Skip` grows, or bulk updates load rows just to modify them
- A filtered/sorted query scans **even though the column is indexed**, or a filtered/sorted column has no supporting index
- A hot, frequently-executed query pays EF Core's LINQ-translation cost on every call

## When Not to Use

- **The code uses Dapper or raw ADO.NET, not EF Core.** Answer the SQL/indexing/query-plan question directly; do not introduce a `DbContext` or recommend `AsNoTracking`, `Include`, `AsSplitQuery`, or other EF Core APIs.

## Source review workflow

1. Find the EF boundary: `DbContext`, `DbSet`, repository implementation, unit of work, query helper, or model-building code. Follow the implementation rather than assuming what an interface method does.
2. Trace each query to execution or enumeration. Check whether operators bind to `Queryable` or `Enumerable`, including inside helpers; an `IEnumerable` can remain a deferred database sequence.
```
END THIRD-PARTY FILE fb62f4ac438ebbc7

The third-party file has ended. Everything between the fb62f4ac438ebbc7 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 2213 of 24565 bytes. © dotnet · MIT · https://github.com/dotnet/skills/tree/HEAD/plugins/dotnet-data/skills/optimizing-ef-core-queries/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
