Skip to content

Fix OriginalValues.ToObject for added complex collections#38493

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
thromel:fix-original-values-complex-collections-added
Jun 27, 2026
Merged

Fix OriginalValues.ToObject for added complex collections#38493
AndriySvyryd merged 1 commit into
dotnet:mainfrom
thromel:fix-original-values-complex-collections-added

Conversation

@thromel

@thromel thromel commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #38486

Problem

OriginalValues.ToObject() can throw for an entity in the Added state 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 before ToObject() can materialize the value graph.

Change

This updates OriginalPropertyValues so complex collection reconstruction uses current complex collection entries when the containing entry is in the Added state. 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 CurrentPropertyValues and those snapshots would not be read.

Tests

Adds a regression test for OriginalValues.ToObject() on an added entity with:

  • a complex collection
  • a nested complex collection inside one of the collection elements
  • assertions that the returned object graph is cloned rather than sharing the tracked complex element instances

Validation:

  • dotnet build test/EFCore.Tests/EFCore.Tests.csproj --no-restore
  • dotnet 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-ansi
  • dotnet artifacts/bin/EFCore.Tests/Debug/net11.0/Microsoft.EntityFrameworkCore.Tests.dll --filter-class Microsoft.EntityFrameworkCore.ChangeTracking.Internal.PropertyValuesTest --no-progress --no-ansi

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 OriginalPropertyValues to use current entries for Added entities.
  • Add a regression test covering OriginalValues.ToObject() on an Added entity 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);
}
@thromel thromel force-pushed the fix-original-values-complex-collections-added branch from aceaff2 to 05a4527 Compare June 27, 2026 00:45
@thromel thromel marked this pull request as ready for review June 27, 2026 00:48
@thromel thromel requested a review from a team as a code owner June 27, 2026 00:48

@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 enabled auto-merge (squash) June 27, 2026 00:52
@AndriySvyryd AndriySvyryd merged commit a7a7184 into dotnet:main Jun 27, 2026
14 checks passed
@thromel thromel deleted the fix-original-values-complex-collections-added branch June 27, 2026 01:25
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jun 28, 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.

OriginalValue access on ComplexCollections not working for Entites in Added State

3 participants