Fix OriginalValues.ToObject for added complex collections#38493
Merged
AndriySvyryd merged 1 commit intoJun 27, 2026
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a regression where OriginalValues.ToObject() throws for entities in the Added state when they contain a complex collection, by falling back to current complex-collection entries (consistent with how original values already fall back to current values for added entities).
Changes:
- Update complex-collection reconstruction in
OriginalPropertyValuesto use current entries forAddedentities. - Add a regression test covering
OriginalValues.ToObject()on anAddedentity with a complex collection (including nested complex collections).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/EFCore.Tests/ChangeTracking/Internal/PropertyValuesTest.cs | Adds a regression test ensuring OriginalValues.ToObject() works for Added entities with complex collections. |
| src/EFCore/ChangeTracking/Internal/OriginalPropertyValues.cs | Adjusts complex-collection element reconstruction to use current entries for Added entities to avoid missing original ordinals. |
Comment on lines
91
to
95
| if (!complexEntry.HasOriginalValuesSnapshot) | ||
| { | ||
| complexEntry.EnsureOriginalValues(); | ||
| SetValuesFromInstance(complexEntry, (IRuntimeTypeBase)complexProperty.ComplexType, element, skipChangeDetection: true); | ||
| } |
aceaff2 to
05a4527
Compare
AndriySvyryd
approved these changes
Jun 27, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
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 #38486
Problem
OriginalValues.ToObject()can throw for an entity in theAddedstate when the entity contains a complex collection. Scalar original values already fall back to current values for added entries, but complex collection reconstruction was still trying to read element entries by original ordinal.For added entities, complex collection elements do not have usable original ordinals. Calling
GetComplexCollectionOriginalEntry(...)therefore throws beforeToObject()can materialize the value graph.Change
This updates
OriginalPropertyValuesso complex collection reconstruction uses current complex collection entries when the containing entry is in theAddedstate. For non-added entries, the existing original-entry reconstruction path is preserved so modified and unchanged entities continue to reconstruct from original snapshots.The added-state branch also avoids creating per-element original snapshots, since the result is materialized through
CurrentPropertyValuesand those snapshots would not be read.Tests
Adds a regression test for
OriginalValues.ToObject()on an added entity with:Validation:
dotnet build test/EFCore.Tests/EFCore.Tests.csproj --no-restoredotnet artifacts/bin/EFCore.Tests/Debug/net11.0/Microsoft.EntityFrameworkCore.Tests.dll --filter-method Microsoft.EntityFrameworkCore.ChangeTracking.Internal.PropertyValuesTest.OriginalValues_ToObject_with_complex_collection_for_added_entity --no-progress --no-ansidotnet artifacts/bin/EFCore.Tests/Debug/net11.0/Microsoft.EntityFrameworkCore.Tests.dll --filter-class Microsoft.EntityFrameworkCore.ChangeTracking.Internal.PropertyValuesTest --no-progress --no-ansi