Skip to content

Better error when using EF.Constant/Parameter with compiled queries or query filters#38155

Merged
roji merged 1 commit intomainfrom
roji/better-error-constant-parameter-compiled
Apr 23, 2026
Merged

Better error when using EF.Constant/Parameter with compiled queries or query filters#38155
roji merged 1 commit intomainfrom
roji/better-error-constant-parameter-compiled

Conversation

@roji
Copy link
Copy Markdown
Member

@roji roji commented Apr 22, 2026

When EF.Constant(), EF.Parameter(), or EF.MultipleParameters() are used inside compiled queries or query filters, a cryptic InvalidCastException was thrown:

System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.ConstantExpression'
  to type 'Microsoft.EntityFrameworkCore.Query.QueryParameterExpression'.

This now throws a clear InvalidOperationException with the message:

'{methodName}' is not supported when using compiled queries or query filters.

The check is placed in ExpressionTreeFuncletizer where the parameterize flag is known to be false for compiled queries and query filters — the two contexts where these methods aren't supported.

Changes

  • ExpressionTreeFuncletizer.cs — Added !_parameterize checks in EF.Constant and HandleParameter (used by EF.Parameter and MultipleParameters) handlers
  • CoreStrings.resx / .Designer.cs — New EFMethodNotSupportedInCompiledQueries resource string
  • Tests — 4 new tests: 2 for compiled queries (NorthwindCompiledQueryTestBase) + 2 for query filters (AdHocQueryFiltersQueryTestBase)

Closes #38151

Copilot AI review requested due to automatic review settings April 22, 2026 14:36
@roji roji requested a review from a team as a code owner April 22, 2026 14:36
@roji roji force-pushed the roji/better-error-constant-parameter-compiled branch from 5f9729f to 22ba473 Compare April 22, 2026 14:38
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Improves diagnostics when EF.Constant(), EF.Parameter() and EF.MultipleParameters() are used in contexts where EF Core disables parameterization (compiled queries and query filters), replacing a cryptic InvalidCastException with a targeted InvalidOperationException and adding coverage for the new behavior.

Changes:

  • Add _parameterize == false guards in ExpressionTreeFuncletizer for EF.Constant and the shared parameter handler (used by EF.Parameter / EF.MultipleParameters).
  • Introduce CoreStrings.EFMethodNotSupportedInCompiledQueries resource string for a clearer error message.
  • Add new tests for compiled queries and query filters to assert the new error.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test/EFCore.SqlServer.FunctionalTests/Query/NorthwindCompiledQuerySqlServerTest.cs Adds required overrides so the SqlServer “all tests overridden” check includes the new compiled-query tests.
test/EFCore.Specification.Tests/Query/NorthwindCompiledQueryTestBase.cs Adds tests asserting clearer exceptions for EF.Constant/EF.Parameter inside compiled queries.
test/EFCore.Specification.Tests/Query/AdHocQueryFiltersQueryTestBase.cs Adds tests asserting clearer exceptions for EF.Constant/EF.Parameter inside query filters.
src/EFCore/Query/Internal/ExpressionTreeFuncletizer.cs Throws a clearer exception when these EF helper methods are invoked while _parameterize is disabled.
src/EFCore/Properties/CoreStrings.resx Adds the new localized error string template.
src/EFCore/Properties/CoreStrings.Designer.cs Adds the generated accessor for the new resource string.
Files not reviewed (1)
  • src/EFCore/Properties/CoreStrings.Designer.cs: Language not supported

Comment thread src/EFCore/Properties/CoreStrings.resx
Comment thread src/EFCore/Query/Internal/ExpressionTreeFuncletizer.cs
…r query filters

When EF.Constant(), EF.Parameter(), or EF.MultipleParameters() are used
inside compiled queries or query filters, throw a clear
InvalidOperationException instead of a cryptic InvalidCastException.

The check is in ExpressionTreeFuncletizer where parameterize is known to
be false for these contexts.

Closes #38151

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@roji roji force-pushed the roji/better-error-constant-parameter-compiled branch from 22ba473 to 4bd878e Compare April 22, 2026 14:57
@AndriySvyryd AndriySvyryd removed their assignment Apr 23, 2026
@roji roji merged commit e87c07b into main Apr 23, 2026
23 checks passed
@roji roji deleted the roji/better-error-constant-parameter-compiled branch April 23, 2026 05:57
@github-actions github-actions Bot added the api-review This PR or issue is introducing public API changes that need to be reviewed label Apr 23, 2026
@github-actions
Copy link
Copy Markdown
Contributor

API review baseline changes for src/EFCore/EFCore.baseline.json

Show diff

The diff below was generated by ApiChief between the base and the PR.

  static class Microsoft.EntityFrameworkCore.Diagnostics.CoreStrings
+ static string EFMethodNotSupportedInCompiledQueries(object? methodName);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api-review This PR or issue is introducing public API changes that need to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better error when trying to use EF.Constant/Parameter with compiled queries and query filters

3 participants