Repository navigation
Fix EF.Constant with a cast from an enum - #39500
Merged
Merged
Conversation
- 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
Contributor
There was a problem hiding this comment.
🔵 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.Constanthandling withEF.Parameterby forcing argument evaluation. - Add a shared Northwind regression test for an enum-to-
intcast. - 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
approved these changes
Nov 3, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #37045
For
EF.Constant((int)enumVar)the argument is aConvertfrom an enum.TryHandleNonEvaluatableAsRootdeliberately doesn't evaluate that as a root, so onlyenumVargets parameterized, theConvertstays, and the argument's state stays evaluatable. The parent==then tries to evaluateEF.Constant(Convert(__id_0))and fails on the unbound variable. Even without that,QueryableMethodNormalizingExpressionVisitorcasts the argument toQueryParameterExpression, so it can't be aConvert.The
EF.Constantcase now passesforceEvaluation: true, the same asHandleParameterdoes forEF.Parameter.Added
EF_Constant_with_enum_casttoNorthwindWhereQueryTestBase. It fails without the change.NorthwindWhereQuerySqlServerTestandNorthwindWhereQueryCosmosTestneed 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.