Skip to content

Query precompiler folds conditionals on captured variables using fake defaults and drops a SQL branch #39428

Closed

Description

@rockyway

Summary

When the query precompiler generates a query, it evaluates captured variables using fake default values and lets the funcletizer simplify the tree. A conditional whose test depends only on captured values is evaluated at generation time, and one arm is dropped. The generated SQL hard-codes the arm the default value picked, so the variable's runtime value is ignored.

The same happens to:

  • a row-independent operand of && / ||;
  • a captured left operand of ??.

Repro

bool all = true;
var rows = ctx.Set<E>().Where(e => all || e.IsActive).ToList();

Precompile the query and publish NativeAOT.

  • Expected (JIT): all rows
  • Actual (precompiled): only the active rows; the SQL filters on IsActive whatever all is

includeInactive ? true : e.IsActive behaves the same way.

Analysis

  • src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs:449-460,1314-1321: captured fields get fake defaults (false, 0, null) during generation.
  • src/EFCore/Query/Internal/ExpressionTreeFuncletizer.cs:510-522: the conditional's test is evaluated and one arm discarded. :366-409 and :416-445 do the same for && / ||, and :376-394 for ??.
  • src/EFCore.Design/Query/Internal/PrecompiledQueryCodeGenerator.cs:779,805-835.

Not affected: a captured bool passed as an ordinary method argument (parameterized by the funcletizer, :1024-1030,1063-1122).

Workaround

Rewrite the guard so the conditional's test depends on the row, e.g. e.IsActive ? true : all instead of all || e.IsActive, or choose a nullable key outside the expression tree. EF.Parameter(...) keeps the SQL arm but then fails in the generated extractor.

Possibly related: #39304.

Environment

  • EF Core 10.0.7 (Microsoft.EntityFrameworkCore.Sqlite 10.0.7, Microsoft.EntityFrameworkCore.Tasks 10.0.7)
  • .NET SDK 10.0.403, PublishAot=true, EFOptimizeContext=true, EFPrecompileQueriesStage=build, win-x64
  • Provider: SQLite
  • Source references below are to the v10.0.7 tag.

Activity

  1. added this to the 12.0.0 milestone on Oct 30, 2026
  2. added theissue type on Oct 30, 2026
  3. davidortinau commented on Nov 5, 2026

    @davidortinau

    +1 from an app preparing to ship EF Core 10 NativeAOT on iOS. These bugs give different results from the same LINQ under the JIT with no error, so a precompiled build cannot be trusted without checking every query.

    We have the reported shape in shared server/mobile code, for example an ownership check

    .Where(resource => resource.UserProfileId == userId
        && (anyOwnedResource || selectedResourceIds.Contains(resource.Id)))

    where anyOwnedResource is a captured bool. With the fake default (false), the bug described here would keep only the Contains arm in the generated SQL, so when anyOwnedResource is true at run time the query would return fewer rows than under the JIT. Today this query fails to precompile for an unrelated reason (captured collection Contains), so fixing that would expose it to this bug.

    Please consider servicing the fix into 10.0.x together with #37323: both are correctness bugs in a feature 10.0 documents.

  4. added a commit that references this issue on Nov 13, 2026
    62d1746
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions