Fix 'ordinal -1 is invalid' crash when a nested JSON complex sub-collection grows#38373
Draft
Copilot wants to merge 4 commits into
Draft
Fix 'ordinal -1 is invalid' crash when a nested JSON complex sub-collection grows#38373Copilot wants to merge 4 commits into
Copilot wants to merge 4 commits into
Conversation
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix InvalidOperationException on SaveChangesAsync for ComplexProperty
Fix 'ordinal -1 is invalid' crash when a nested JSON complex sub-collection grows
Jun 6, 2026
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.
An entity with a JSON-mapped complex property whose nested collection items define 2+ sub-collections would throw on save when one sub-collection grew between load and save:
Root cause
InternalEntryBase.SetPropertyModified'srecurseloop forced every flattened complex-collection element — includingAddedones — toModified/Unchanged. For a newly-grown element (anAddedentry, whose original ordinal is legitimately-1),InternalComplexEntry.SetEntityStatethen ranValidateOrdinal(original: true), which throws.Changes
InternalEntryBase.SetPropertyModified: skipAddedcomplex entries in the recurse loop, mirroring the guard already present in the bulkInternalComplexCollectionEntry.SetEntityState. Their state is left as computed by change detection.Quirk switch: gated behind
Microsoft.EntityFrameworkCore.Issue38299(UseOldBehavior38299), consistent with servicing precedent (Can't change state of entity with complex collection #37724/Deleting an item from a ComplexCollection, that contains an array results in Error #37585).Tests: added
Grow_nested_sub_collection_in_complex_property_mapped_to_jsonto the relationalComplexCollectionJsonUpdateTestBase(newWidgetWithDeepJsonmodel: ToJson → wrapper → item collection → item with two sub-collections), plus the SQLite override with SQL baseline.Reviewer note
The bug's reproduction is sensitive to the global complex-collection index ordering (name/structure dependent). The fix is verified end-to-end against a standalone repro matching the issue's model in both directions (crashes without the guard, persists the grown collection with it). The added harness test exercises the grow-and-replace path; a reviewer may want to consider strengthening it (e.g. adding the second JSON column noted in the issue) so it fails hard without the fix rather than serving primarily as a feature test.