Documentation
EF Core Analyzer Rules
A practical guide to LinqContraband's 45 EF Core analyzer rules for query performance, loading, tracking, async execution, raw SQL safety, and CI policy.
EF Core Analyzer Rules
LinqContraband provides 45 EF Core analyzer rules for teams that want repeatable query review in the IDE and CI. The rules cover LINQ query shape, materialization, loading, async execution, tracking, bulk operations, schema modeling, and raw SQL safety.
Install the official analyzer package:
dotnet add package LinqContraband
Rule Families
Use these groups to decide which EF Core diagnostics are most useful for your project before diving into the full rule catalog.
| Rule family | Use it when you want to catch | Starting rules |
|---|---|---|
| Query shape and translation | Local methods, unstable ordering, non-translatable overloads, and query shapes that can fall out of SQL translation. | EF Core client-side evaluation analyzer, LC001: local method, EF Core pagination OrderBy analyzer |
| Materialization and projection | Early ToList, whole-entity fetches, unbounded result sets, nested collection materializers, and scalar reads that should project in SQL. |
EF Core projection analyzer, LC017: whole entity projection, EF Core premature materialization analyzer |
| Loading and includes | Missing includes, cartesian explosion, excessive eager loading, deep include chains, and untagged complex queries. | LC045: missing include, LC006: cartesian explosion, EF Core Include analyzer |
| Execution and async | Database work inside loops, synchronous EF Core calls in async paths, repeated saves, missing cancellation tokens, and async-stream buffering. | EF Core async query analyzer, EF Core CancellationToken analyzer, LC026: missing cancellation token |
| Tracking and context lifetime | Missing AsNoTracking, no-tracking writes, mixed tracking modes, repeated SaveChanges, and DbContext lifetime mistakes. |
LC030: DbContext lifetime, LC036: context captured across threads, EF Core DbContext lifetime analyzer |
| Bulk operations and modeling | Set-based write opportunities, unbounded bulk updates or deletes, missing keys, and missing explicit foreign keys. | LC032: ExecuteUpdate, LC035: missing Where before bulk execute, EF Core ExecuteUpdate analyzer |
| Raw SQL and security | Interpolated raw SQL, constructed SQL strings, unsafe command SQL, and query-filter bypasses. | LC018: interpolated raw SQL, LC034: interpolated command SQL, LC021: IgnoreQueryFilters |
Rules To Enable First
For a low-noise rollout, start with the rules that usually represent clear production risk:
[*.cs]
# Repeated database work and missing related data
dotnet_diagnostic.LC007.severity = error
dotnet_diagnostic.LC045.severity = warning
# Raw SQL and query-filter bypasses
dotnet_diagnostic.LC018.severity = error
dotnet_diagnostic.LC034.severity = error
dotnet_diagnostic.LC037.severity = error
dotnet_diagnostic.LC021.severity = warning
# Expensive query shapes
dotnet_diagnostic.LC001.severity = warning
dotnet_diagnostic.LC002.severity = warning
dotnet_diagnostic.LC014.severity = warning
dotnet_diagnostic.LC015.severity = warning
dotnet_diagnostic.LC020.severity = warning
dotnet_diagnostic.LC024.severity = warning
dotnet_diagnostic.LC031.severity = warning
dotnet_diagnostic.LC036.severity = warning
# Async EF Core execution
dotnet_diagnostic.LC008.severity = warning
dotnet_diagnostic.LC026.severity = suggestion
dotnet_diagnostic.LC043.severity = suggestion
Keep broader design guidance as warnings until the team has reviewed existing findings. Promote a rule to error only
when it represents project policy and developers have a documented exception path.
Where Each Rule Fits
- Use the EF Core query performance checklist when you need a reviewer-friendly pull-request aid.
- Use the EF Core client-side evaluation analyzer guide
when local methods in
IQueryable, early client boundaries, string comparison overloads, or non-translatableGroupByprojections are the main concern. - Use the EF Core pagination OrderBy analyzer guide when missing
OrderBybeforeSkip,Take,Last,ElementAt, orChunkis the main concern. - Use the EF Core DbContext lifetime analyzer guide when scoped lifetime, singleton services, background workers, disposed queries, or cross-thread capture are the main concern.
- Use the EF Core async query analyzer guide when sync-over-async,
missing cancellation tokens,
ToListAsync, async streams, orSaveChangesAsyncloops are the main concern. - Use the EF Core CancellationToken analyzer guide when
ToListAsync,FirstOrDefaultAsync,SaveChangesAsync, or other async EF Core calls should receive the available operation token. - Use the EF Core premature materialization analyzer guide
when early
ToList,AsEnumerable, unbounded materialization, or projection waste are the main concern. - Use the EF Core projection analyzer guide when whole-entity loads,
scalar projection, nested
ToListinsideSelect, or redundant identitySelectcalls are the main concern. - Use the EF Core Include analyzer guide when missing related data, cartesian explosion, or over-eager loading are the main concern.
- Use the EF Core N+1 query detector guide when repeated database calls or loading strategy are the main concern.
- Use the EF Core raw SQL injection analyzer guide when raw SQL, command SQL, or query-filter bypasses need stronger review.
- Use the EF Core AsNoTracking analyzer guide when read-only query tracking, mixed tracking modes, or no-tracking write paths are the main concern.
- Use the EF Core ExecuteUpdate analyzer guide when tracked bulk
loops,
RemoveRangedeletes, or unfiltered set-based writes are the main concern. - Use the EF Core SaveChanges in loop analyzer guide when repeated saves, N+1 writes, or batched write policy are the main concern.
- Use the EF Core query analyzer CI guide when selected rules should warn or fail pull requests.
- Use the full rule catalog for every rule’s severity, category, sample folder, and code-fix status.
Official Links
- Canonical repository: github.com/georgepwall1991/LinqContraband
- Official NuGet package: nuget.org/packages/LinqContraband
- Documentation hub: georgepwall1991.github.io/LinqContraband
- Safe install guidance: Official LinqContraband downloads and authenticity