optimizing-ef-core-queries

optimizing-ef-core-queries is a skill for Claude Code, Codex from managedcode/dotnet-skills. It costs 86 tokens per session (2,527 once invoked), scanned A, a copy of optimizing-ef-core-queries, MIT.

A guide to finding and fixing slow Entity Framework Core database queries. EF Core is a .NET tool that turns C# queries into database commands.

In plain words
What is it for?
Use it to inspect generated SQL, reduce N+1 queries and round-trips, improve paging and bulk updates, and measure whether a query change helped.
Why use it?
It helps identify excessive SQL, repeated database calls, duplicated rows, missing indexes, slow pagination, and other causes of poor query performance.

Skill for Claude CodeCodex

Written for no agent in particular: nothing here depends on one.

Good fit Use it to inspect generated SQL, reduce N+1 queries and round-trips, improve paging and bulk updates, and measure whether a query change helped.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/managedcode/dotnet-skills/optimizing-ef-core-queries
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Any agent
npx skills add managedcode/dotnet-skills --skill optimizing-ef-core-queries
Clone the repo
git clone --depth 1 https://github.com/managedcode/dotnet-skills

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for optimizing-ef-core-queries

README.md
[![agentmods](https://agentmods.dev/badge/skills/managedcode/dotnet-skills/optimizing-ef-core-queries.svg)](https://agentmods.dev/skills/managedcode/dotnet-skills/optimizing-ef-core-queries)
Your own site
<a href="https://agentmods.dev/skills/managedcode/dotnet-skills/optimizing-ef-core-queries"><img src="https://agentmods.dev/badge/skills/managedcode/dotnet-skills/optimizing-ef-core-queries.svg" alt="Measured on agentmods" height="20"></a>
Per session 86 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,527 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin 100% copy Near-identical to another mod in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00086 $0.02527
Opus 5 $0.00043 $0.01264
Sonnet 5 $0.00017 $0.00505
Haiku 4.5 $0.00009 $0.00253

Measured 4d ago against content hash 674fe2a77386, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-07, from the pricing page.

Security

Grade A, and why

optimizing-ef-core-queries scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 4d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

Origin

This is a copy

100% identical to optimizing-ef-core-queries — 0 lines differ, which has more behind it and is treated as the original. This page carries a canonical link to it rather than competing with it.

catalog/Libraries/Official-DotNet-Data/skills/optimizing-ef-core-queries/SKILL.md · 160 lines

How it starts

The opening of the file, as written. The whole thing — 160 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Optimizing EF Core Queries

Diagnose and fix slow Entity Framework Core (EF Core) queries. Start from the generated SQL/logs, apply the smallest change that removes the bottleneck, and confirm the fix by re-reading the SQL and the query count. Prefer changes that reduce round-trips, duplicated rows, scans, or per-call translation cost over micro-optimizations. Apply one change at a time and re-measure.

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 Includes 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.

First: capture the generated SQL

You cannot optimize what you cannot see. Turn on command logging and read the SQL and query count before changing anything:

optionsBuilder.LogTo(Console.WriteLine, LogLevel.Information);
// or set "Microsoft.EntityFrameworkCore.Database.Command": "Information" in appsettings.json

Tag a query with .TagWith("...") to find it in the log. Count how many statements a slow operation runs, and how many rows each returns, before and after each change.

Fixes

Keep predicates sargable — never wrap an indexed column in a function

An index can only be used when the indexed column appears bare on one side of the comparison. Wrapping it in a function or arithmetic — CreatedAt.Year == y, CreatedAt.Date == d, ToLower(Name) == n, Price * 1.1 > x, or a leading-wildcard LIKE '%foo' — forces a per-row computation the index cannot satisfy, so the query scans the whole table even though the index exists. Adding another index changes nothing. Rewrite the predicate so the column stays bare, usually as a half-open range:

Read the full file on GitHub · 160 lines

Files

What ships with it

1 file beside SKILL.md in the same directory: the scripts, references and assets a skill reads on demand. Not counted in the per-session cost; read them before you install if any of them is executable.

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 4d ago First seen · 160 lines · 86 tokens per session scan A 674fe2a77386

Subscribe to this mod's changes

optimizing-ef-core-queries is a skill published in the GitHub repository managedcode/dotnet-skills (477 stars, last pushed 2d ago), licensed MIT. It adds 86 tokens to every session and 2,527 once invoked, about $0.0004 per session on Opus 5. A static security scan graded it A with 0 findings. It is 100% identical to optimizing-ef-core-queries, differing in 0 lines, and is treated as a copy.