Repository navigation
[release/11.0] Prevent data loss when DetectChanges runs before saving JSON complex collections - #39778
Open
AndriySvyryd wants to merge 3 commits into
Open
[release/11.0] Prevent data loss when DetectChanges runs before saving JSON complex collections#39778AndriySvyryd wants to merge 3 commits into
AndriySvyryd wants to merge 3 commits into
Conversation
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
force-pushed
the
copilot/fix-json-complex-collection
branch
from
November 24, 2026 01:44
e9ba983 to
8ce6ba2
Compare
Contributor
There was a problem hiding this comment.
🟢 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.Issue39450switch 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
Code coverage overviewLanguages: C# C# / code-coverage
View coverage of the 10 most impacted files.
ℹ️ Calculations represent line coverage. Results may change as calculations are updated. |
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 #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 beforeSaveChanges(), including through calls such asChangeTracker.Entries(). A workaround is to avoid those calls before saving; normal automatic detection duringSaveChanges()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.Issue39450totrueto retain the old behavior.