[release/10.0] Fix ArgumentOutOfRangeException for entities with shadow properties and nullable complex types with discriminators#37394
Merged
AndriySvyryd merged 1 commit intorelease/10.0from Jan 6, 2026
Conversation
Copilot
AI
changed the title
[WIP] Fix ArgumentOutOfRangeException for entity with shadow property
Fix ArgumentOutOfRangeException for entities with shadow properties and nullable complex types with discriminators
Dec 18, 2025
test/EFCore.InMemory.FunctionalTests/Query/AdHocComplexTypeQueryInMemoryTest.cs
Outdated
Show resolved
Hide resolved
Member
|
@copilot Update progress by rewriting the PR summary using this template: |
There was a problem hiding this comment.
Pull request overview
This PR fixes an ArgumentOutOfRangeException that occurs when querying entities that have both shadow properties and nullable complex types with discriminators (issue #37337). The fix ensures that shadow properties on complex types are replaced with default values during snapshot creation, since shadow property materialization on complex types is not currently supported in EF Core.
Key Changes:
- Added an AppContext switch
Microsoft.EntityFrameworkCore.Issue37337for backward compatibility control - Modified
SnapshotFactoryFactory.CreateSnapshotExpressionto handle shadow properties on complex types by using default values instead of attempting materialization - Added comprehensive test coverage to verify the fix works correctly
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
| src/EFCore/ChangeTracking/Internal/SnapshotFactoryFactory.cs | Implements the fix by detecting shadow properties on complex types and substituting them with default values during snapshot creation, controlled by an AppContext switch |
| test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs | Adds a test that reproduces the scenario with shadow properties, nullable complex types with discriminators, and verifies the fix works correctly |
test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs
Outdated
Show resolved
Hide resolved
…ies on complex types in snapshot creation Fixes #37337 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
a6d621a to
9501372
Compare
SamMonoRT
approved these changes
Jan 5, 2026
roji
approved these changes
Jan 6, 2026
artl93
approved these changes
Jan 6, 2026
Member
artl93
left a comment
There was a problem hiding this comment.
Customer reported, new feature, low risk. Approved.
This was referenced Jan 7, 2026
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #37337
Description
EF doesn't currently support shadow properties on complex types. However, the discriminator property is special-cased (it's used internally by EF to distinguish null vs empty complex properties). Unfortunately, there was a code path missed.
Customer impact
Applications that use a discriminator on a nullable complex property will fail at runtime when querying the affected entities. There is no good workaround for this.
How found
Customer reported on EF 10.0.0, this is a relatively common scenario for optional complex properties.
Regression
No, new feature in EF 10.
Testing
Test added.
Risk
Low. The fix is targeted and quirked.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.