Skip to content

[release/11.0] Prevent data loss when DetectChanges runs before saving JSON complex collections - #39778

Open
AndriySvyryd wants to merge 3 commits into
release/11.0from
copilot/fix-json-complex-collection
Open

AndriySvyryd wants to merge 3 commits into
release/11.0from
copilot/fix-json-complex-collection

Conversation

@AndriySvyryd

@AndriySvyryd AndriySvyryd commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Fixes #39450

Description
When a JSON-mapped complex collection is replaced with a list that prepends a new item, reuses existing tracked elements, and drops the tail, change detection can shift the reused entries incorrectly. A later save then persists the old first item twice and loses the new item. The fix preserves matched element ordinals when moving entries across slots.

Customer impact
Users can silently lose new data in the above scenario. The issue occurs when DetectChanges() runs before SaveChanges(), including through calls such as ChangeTracker.Entries(). A workaround is to avoid those calls before saving; normal automatic detection during SaveChanges() does not exhibit the problem.

How found
User reported on EF Core 11.0.0-rc.3.

Regression
No.

Testing
3 tests added.

Risk
Low. The change is limited to complex-collection change detection and has focused provider coverage. Quirk added: set Microsoft.EntityFrameworkCore.Issue39450 to true to retain the old behavior.

@AndriySvyryd AndriySvyryd changed the title [WIP] Fix issue with DetectChanges and SaveChanges in complex collection Prevent data loss when DetectChanges runs before saving JSON complex collections Nov 24, 2026
@AndriySvyryd AndriySvyryd changed the title Prevent data loss when DetectChanges runs before saving JSON complex collections [release/11.0] Prevent data loss when DetectChanges runs before saving JSON complex collections Nov 24, 2026
AndriySvyryd and others added 3 commits November 23, 2026 17:35
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Allow moved entries after newly added slots and use the release branch's nested JSON collection model in regression tests.
@AndriySvyryd
AndriySvyryd changed the base branch from main to release/11.0 November 24, 2026 01:44
@AndriySvyryd
AndriySvyryd force-pushed the copilot/fix-json-complex-collection branch from e9ba983 to 8ce6ba2 Compare November 24, 2026 01:44
@AndriySvyryd
AndriySvyryd requested a balanced review from Copilot November 24, 2026 01:49

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

There are no active review issues blocking approval of the JSON collection change-detection fix.

No open issues 🎉

What changed in this PR

This servicing fix addresses #39450 by preventing silent data loss when change detection runs before saving a JSON complex collection that prepends a new element and reuses existing elements.

Changes:

  • Preserve already-matched element ordinals when moving reused collection entries.
  • Add the Microsoft.EntityFrameworkCore.Issue39450 switch to restore the previous behavior.
  • Add regression coverage for top-level and nested JSON collections, with SQL Server and SQLite SQL assertions.
File Description
test/​EFCore.SqlServer.FunctionalTests/​Update/​ComplexCollectionJsonUpdateSqlServerTest.cs Assert SQL Server persists the expected collection contents.
test/​EFCore.Sqlite.FunctionalTests/​Update/​ComplexCollectionJsonUpdateSqliteTest.cs Assert SQLite persists the expected collection contents.
test/​EFCore.Relational.Specification.Tests/​Update/​ComplexCollectionJsonUpdateTestBase.cs Verify prepended elements survive explicit change detection and saving.
src/​EFCore/​ChangeTracking/​Internal/​ChangeDetector.cs Correct collection entry movement and add the compatibility switch.

Review effort: Balanced

@github-code-quality

Copy link
Copy Markdown

Code coverage overview

Languages: C#

C# / code-coverage

release/11.0 90fd1a7 copilot/fix-json-com... 8ce6ba2 +/-
76% 67% -9%
View coverage of the 10 most impacted files.
File release/11.0 90fd1a7 copilot/fix-json-com... 8ce6ba2 +/-
/Users/runner/w...nal/Snapshot.cs 0% 60% +60%
D:\a\efcore\efc...nal\Snapshot.cs 0% 64% +64%
/home/runner/wo...nal/Snapshot.cs 0% 66% +66%
/home/runner/wo...yTypeBuilder.cs 0% 88% +88%
D:\a\efcore\efc...yTypeBuilder.cs 0% 88% +88%
/Users/runner/w...ngs.Designer.cs 0% 92% +92%
/home/runner/wo...ctExpression.cs 0% 93% +93%
D:\a\efcore\efc...ctExpression.cs 0% 93% +93%
D:\a\efcore\efc...ngs.Designer.cs 0% 94% +94%
/home/runner/wo...ngs.Designer.cs 0% 95% +95%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.

@AndriySvyryd
AndriySvyryd marked this pull request as ready for review November 25, 2026 23:49
@AndriySvyryd
AndriySvyryd requested a review from a team as a code owner November 25, 2026 23:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ToJson complex collection: an explicit DetectChanges() before SaveChanges() silently loses a prepended element and writes the old head twice

3 participants