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