Analyzer rule

LC013: Disposed Context Query Leak

LC013 detects an IQueryable or IAsyncEnumerable returned after its DbContext is disposed, which throws when the caller finally enumerates it.

Default severity
Warning
Code fix
Manual only
Category
Reliability

Change the severity in .editorconfig:

[*.cs]
dotnet_diagnostic.LC013.severity = error

Sample code · Analyzer source

LC013: Disposed Context Query Leak

In Plain Terms

Imagine buying a ticket to a movie. But the ticket is only valid inside the ticket booth. As soon as you walk out to the theater (return the query), the ticket dissolves in your hand.

Goal

Detect when an IQueryable or IAsyncEnumerable is returned from a method while the DbContext that created it is disposed.

The Problem

IQueryable and IAsyncEnumerable use deferred execution. If the DbContext is disposed (e.g., at the end of a using block), any attempt to run the query later will result in an ObjectDisposedException.

Example Violation

public IQueryable<User> GetUsers()
{
    using var db = new AppDbContext();
    var query = db.Users.Where(u => u.Active);

    // Violation: Caller cannot use this query because db is about to be disposed
    return query;
}

The Fix

Materialize the query before returning, or ensure the context lifetime is managed correctly (e.g., by the DI container).

public List<User> GetUsers()
{
    using var db = new AppDbContext();
    // Correct: Data is fetched before context is disposed
    return db.Users.Where(u => u.Active).ToList();
}

Analyzer Logic

  • Tracks the returned query back through single-assignment local aliases in the same executable root.
  • Handles conditional, coalesce, and switch-expression returns branch by branch.
  • Only reports when the origin is a disposed local DbContext.
  • Continues through known LINQ/EF query-chain operators, but treats arbitrary project extension methods as boundaries because they may materialize before returning IQueryable.
  • Ignores nested local-function and lambda returns to avoid false positives when the outer method materializes the query before exiting.
  • No automatic code fix is offered for LC013 in this pass.

ID: LC013

Category: Reliability

Severity: Warning