Skip to content

Fix JSON complex property projections with identity resolution - #39442

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
thalysmarciobn:fix/39322-json-complex-type-identity-resolution
Oct 30, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
thalysmarciobn:fix/39322-json-complex-type-identity-resolution

Conversation

@thalysmarciobn

@thalysmarciobn thalysmarciobn commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Projecting a JSON-mapped complex property with AsNoTrackingWithIdentityResolution() throws an InvalidCastException because the JSON entity order validator casts complex type metadata to IEntityType.

Restrict entity order validation to entity types, allowing complex properties to be materialized without treating them as entities with independent identity.

Add regression tests for anonymous and member-initializer projections, covering required and optional complex properties with synchronous and asynchronous execution on SQLite and SQL Server.

Validation:

  • All four new SQLite regression cases fail before the fix and pass afterward.
  • 225 related tests pass across SQLite and SQL Server, with no skips.

Fixes #39322

- Restrict JSON entity order validation to entity types
- Add sync and async projection regression tests for SQLite and SQL Server

Fixes dotnet#39322
@thalysmarciobn

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

No active review issues are identified for the targeted JSON complex-property projection fix.

No open issues 🎉

What changed in this PR

This pull request fixes an InvalidCastException when projecting JSON-mapped complex properties with AsNoTrackingWithIdentityResolution(). Entity-order validation is now limited to entity metadata, allowing complex properties to materialize correctly.

Changes:

  • Restricts JSON identity-order validation to entity shapers.
  • Adds synchronous and asynchronous regression tests for anonymous and member-initializer projections.
  • Adds SQLite and SQL Server SQL baselines covering required and optional complex properties.
File Description
test/​EFCore.SqlServer.FunctionalTests/​Query/​Associations/​ComplexJson/​ComplexJsonProjectionSqlServerTest.cs Adds SQL Server SQL baselines for the new regression tests.
test/​EFCore.Sqlite.FunctionalTests/​Query/​Associations/​ComplexJson/​ComplexJsonProjectionSqliteTest.cs Adds SQLite SQL baselines for the new regression tests.
test/​EFCore.Relational.Specification.Tests/​Query/​Associations/​ComplexJson/​ComplexJsonProjectionRelationalTestBase.cs Adds shared regression tests for required and optional JSON complex properties.
src/​EFCore.Relational/​Query/​RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs Restricts identity-resolution JSON ordering validation to entity shapers.

Review effort: Lite

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

@AndriySvyryd
AndriySvyryd merged commit f29db88 into dotnet:main Oct 30, 2026
20 of 21 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 31, 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.

Projecting a JSON-mapped complex property with AsNoTrackingWithIdentityResolution throws InvalidCastException

3 participants