Skip to content

Fix precompiled queries for sealed entity types - #39326

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
nickna:codex/fix-37957-sealed-materialization
Oct 22, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
nickna:codex/fix-37957-sealed-materialization

Conversation

@nickna

@nickna nickna commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Generated precompiled-query interceptors emit an IInjectableService type check and cast even when the materialized type is sealed and does not implement that interface. The generated C# then produces CS0184/CS0030.

Skip attachment expressions only for sealed types that cannot implement IInjectableService. Unsealed types and sealed interface implementations retain their existing callbacks.

Adds shared relational regressions which compile the generated interceptors, execute queries, assert materialized values, and verify that a sealed injectable entity receives exactly one Injected callback. Includes SQL Server overrides and SQL assertions.

Fixes #37957.

Verification

Windows x64, using the repository-pinned SDK and test runtimes:

  • Before the fix, the plain sealed-entity regression fails with generated-code CS0030; the injectable-service control passes (2 tests, 1 failure).
  • After the fix, both regressions pass with generated-query execution.
  • SQLite precompiled-query suites: 155 passed, 4 existing skips for JSON column translation fails when using .Union() #33378.
  • Core EntityMaterializerSourceTest: 18 passed.
  • SQL Server test project builds; Check_all_tests_overridden passes (1 test), executed directly with database configuration disabled. SQL Server database tests were not run locally.
  • git diff --check passes.

The SQLite and core commands used were:

dotnet test --project test/EFCore.Sqlite.FunctionalTests/EFCore.Sqlite.FunctionalTests.csproj --minimum-expected-tests 2 -- --filter-method '*AdHocPrecompiledQuerySqliteTest.Materialize_sealed_entity*'
dotnet test --project test/EFCore.Sqlite.FunctionalTests/EFCore.Sqlite.FunctionalTests.csproj --no-build --minimum-expected-tests 2 -- --filter-class '*PrecompiledQuerySqliteTest'
dotnet test --project test/EFCore.Tests/EFCore.Tests.csproj --minimum-expected-tests 1 -- --filter-class '*EntityMaterializerSourceTest'

…ypes

Preserve injectable-service callbacks and verify sealed-entity precompiled queries with relational regressions.

Fixes dotnet#37957
@nickna
nickna requested a review from a team as a code owner October 18, 2026 19:46
@nickna

nickna commented Oct 18, 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.

🟢 Approval recommended

The guard excludes only impossible interface matches, and focused regressions cover compilation, execution, and callback preservation.

No open issues 🎉

What changed in this PR

Fixes #37957 by preventing EF Core precompiled queries from generating invalid IInjectableService checks and casts for sealed types that cannot implement the interface.

Changes:

  • Skips impossible service callbacks while preserving behavior for unsealed types and sealed interface implementations.
  • Adds shared relational regressions covering generated-code compilation, query execution, materialized values, and exactly one injection callback.
  • Adds SQL Server overrides with SQL assertions.
File Description
test/​EFCore.SqlServer.FunctionalTests/​Query/​AdHocPrecompiledQuerySqlServerTest.cs Adds provider overrides and SQL baselines for both regressions.
test/​EFCore.Relational.Specification.Tests/​Query/​AdHocPrecompiledQueryRelationalTestBase.cs Adds sealed-entity regressions and injectable callback verification.
src/​EFCore/​Query/​Internal/​StructuralTypeMaterializerSource.cs Omits interface checks and callbacks for incompatible sealed types.

Review effort: Balanced

@AndriySvyryd

Copy link
Copy Markdown
Member

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd enabled auto-merge (squash) October 22, 2026 00:04
@AndriySvyryd
AndriySvyryd merged commit ae8f5c4 into dotnet:main Oct 22, 2026
20 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 22, 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.

Sealed entity types lead to error and warning when trying to cast to IInjectableService in precompiled queries

3 participants