Skip to content

Fix precompiled queries not capturing parameter symbol - #39149

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
jksware:fix/precompiled-queries-capture
Oct 2, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
jksware:fix/precompiled-queries-capture

Conversation

@jksware

@jksware jksware commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

The proposed changes solve the parameter capture issue (an IParameterSymbol case was missing) when trying to precompile queries.

This solves the following error:

System.InvalidOperationException: Dynamic LINQ queries are not supported when precompiling queries

when trying to compile:

static async Task<List<Blog>> GetBlogsById(PrecompiledQueryContext parameterContext, int id)
    => await parameterContext.Blogs.Where(b => b.Id == id).ToListAsync();

due to parameters parameterContext and id not being recognized as valid captured symbols.

I've added some unit tests to account for regular argument capturing and DbContext capturing from a parameter symbol.

Fixes #35887

  • 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:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

@jksware
jksware requested a review from a team as a code owner October 2, 2026 15:04
@jksware

jksware commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

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.

Copilot review overview

🟢 Approval recommended

The focused implementation covers both missing symbol paths with end-to-end regression tests and matching SQL assertions.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes precompiled query discovery and translation for method parameters, including DbContext parameters and values captured by query lambdas.

Changes:

  • Recognizes parameter symbols during query discovery and LINQ translation.
  • Adds relational coverage and SQL Server baselines for both scenarios.
File Description
src/​EFCore.Design/​Query/​Internal/​QueryLocator.cs Recognizes parameter-based DbContext query roots.
src/​EFCore.Design/​Query/​Internal/​CSharpToLinqTranslator.cs Translates captured method parameters.
test/​EFCore.Relational.Specification.Tests/​Query/​PrecompiledQueryRelationalTestBase.cs Adds provider-independent regression tests.
test/​EFCore.SqlServer.FunctionalTests/​Query/​PrecompiledQuerySqlServerTest.cs Adds expected SQL baselines.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@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!

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.

Precompiled Queries fail for captured parameters

3 participants