Skip to content

[release/10.0] Fix projection of entities with complex collections through subqueries#37747

Merged
roji merged 1 commit intodotnet:release/10.0from
roji:37725
Feb 24, 2026
Merged

[release/10.0] Fix projection of entities with complex collections through subqueries#37747
roji merged 1 commit intodotnet:release/10.0from
roji:37725

Conversation

@roji
Copy link
Copy Markdown
Member

@roji roji commented Feb 19, 2026

Fixes #37725

Description

Projecting an entity with a complex collection (mapped to JSON via ComplexCollection + ToJson() through a subquery (e.g., FirstOrDefault() throws UnreachableException because ClientProjectionRemappingExpressionVisitor does not handle IComplexProperty when extracting the navigation from a CollectionResultExpression.StructuralProperty.

Customer impact

Customers who migrated from owned entity types to complex collection types (as recommended in the EF 10 release notes) cannot project entities with complex collections through subqueries. The query throws UnreachableException: The program executed an instruction that was thought to be unreachable.

Querying the entity directly (without going through a parent navigation/subquery) works fine. There is no workaround other than reverting to owned entity types or restructuring queries to avoid subquery projections.

How found

Customer reported on 10.0

Regression

No, complex collections mapped to JSON is a new feature in EF 10.

Testing

Test added.

Risk

Extremely low; single line fix to handle a case that otherwise immediately throws UnreachableException (quirk makes no sense).

@roji roji requested a review from a team as a code owner February 19, 2026 17:29
Copilot AI review requested due to automatic review settings February 19, 2026 17:29
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

This PR fixes a bug where projecting entities with complex collections (mapped to JSON) through subqueries throws an UnreachableException. The issue was introduced with the new complex collections mapped to JSON feature in EF 10, where ClientProjectionRemappingExpressionVisitor didn't handle IComplexProperty when processing CollectionResultExpression.StructuralProperty.

Changes:

  • Fixed ClientProjectionRemappingExpressionVisitor to handle IComplexProperty by treating it like null (since complex properties aren't navigations)
  • Added test coverage for projecting complex collections through subqueries

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/EFCore.Relational/Query/SqlExpressions/SelectExpression.Helper.cs Fixed switch expression to handle IComplexProperty case by treating it as null navigation
test/EFCore.Specification.Tests/Query/Associations/AssociationsProjectionTestBase.cs Added base test for selecting complex collection through subquery with FirstOrDefault
test/EFCore.SqlServer.FunctionalTests/Query/Associations/ComplexJson/ComplexJsonProjectionSqlServerTest.cs Added SQL Server test implementation with SQL baseline verification
test/EFCore.Sqlite.FunctionalTests/Query/Associations/ComplexJson/ComplexJsonProjectionSqliteTest.cs Added SQLite test override that correctly expects OUTER APPLY not supported error

@roji roji changed the title Fix projection of entities with complex collections through subqueries [release/10.0] Fix projection of entities with complex collections through subqueries Feb 19, 2026
@AndriySvyryd AndriySvyryd requested a review from artl93 February 20, 2026 00:35
Copy link
Copy Markdown
Member

@artl93 artl93 left a comment

Choose a reason for hiding this comment

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

New scenario. Customer reported. Approved. That doesn't look like a reasonable workaround.

@rbhanda rbhanda added this to the 10.0.5 milestone Feb 23, 2026
@roji roji merged commit ebc7591 into dotnet:release/10.0 Feb 24, 2026
9 checks passed
@roji roji deleted the 37725 branch February 24, 2026 11:11
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.

5 participants