Skip to content

Fix EF.Constant with a cast from an enum - #39500

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
AsadIqbal-Dev:fix/ef-constant-enum-cast
Nov 3, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
AsadIqbal-Dev:fix/ef-constant-enum-cast

Conversation

@AsadIqbal-Dev

Copy link
Copy Markdown
Contributor
  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

Fixes #37045

For EF.Constant((int)enumVar) the argument is a Convert from an enum. TryHandleNonEvaluatableAsRoot deliberately doesn't evaluate that as a root, so only enumVar gets parameterized, the Convert stays, and the argument's state stays evaluatable. The parent == then tries to evaluate EF.Constant(Convert(__id_0)) and fails on the unbound variable. Even without that, QueryableMethodNormalizingExpressionVisitor casts the argument to QueryParameterExpression, so it can't be a Convert.

The EF.Constant case now passes forceEvaluation: true, the same as HandleParameter does for EF.Parameter.

Added EF_Constant_with_enum_cast to NorthwindWhereQueryTestBase. It fails without the change.

NorthwindWhereQuerySqlServerTest and NorthwindWhereQueryCosmosTest need overrides of the new test. I don't have SQL Server or the Cosmos emulator locally, so those two compile but haven't run here. The SQL Server baseline matches what the query generates on SQLite, WHERE "o"."OrderID" = 10248. All Northwind tests on SQLite pass.

- EF.Constant now forces evaluation of its argument, like EF.Parameter does
- A Convert from an enum is not evaluated as a root, so only its operand was parameterized and the unbound parameter was evaluated later

Fixes dotnet#37045

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Because the change affects shared query funcletization and provider-specific SQL/Cosmos paths were not runtime-validated locally, human verification is needed for the SQL baseline and EF.Constant edge cases across supported providers.

No open issues 🎉

What changed in this PR

This pull request fixes EF.Constant when its argument is a cast from a captured enum value by forcing the complete argument to be evaluated before parameterization.

Changes:

  • Align EF.Constant handling with EF.Parameter by forcing argument evaluation.
  • Add a shared Northwind regression test for an enum-to-int cast.
  • Add SQL Server SQL-baseline and Cosmos provider-specific test overrides.
File Description
test/​EFCore.SqlServer.FunctionalTests/​Query/​NorthwindWhereQuerySqlServerTest.cs Adds the SQL Server baseline for the regression test.
test/​EFCore.Specification.Tests/​Query/​NorthwindWhereQueryTestBase.cs Adds the shared enum-cast regression test.
test/​EFCore.Cosmos.FunctionalTests/​Query/​NorthwindWhereQueryCosmosTest.cs Preserves Cosmos’s documented unsupported-provider behavior.
src/​EFCore/​Query/​Internal/​ExpressionTreeFuncletizer.cs Forces complete evaluation of EF.Constant arguments before parameterization.

Review effort: Lite

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit 7284a9c into dotnet:main Nov 3, 2026
19 of 20 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Regression] Unexpected unbound variable error

3 participants