Skip to content

Fix 'ordinal -1 is invalid' crash when a nested JSON complex sub-collection grows#38373

Draft
Copilot wants to merge 4 commits into
release/10.0from
copilot/fix-invalid-ordinal-error
Draft

Fix 'ordinal -1 is invalid' crash when a nested JSON complex sub-collection grows#38373
Copilot wants to merge 4 commits into
release/10.0from
copilot/fix-invalid-ordinal-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 6, 2026

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:

System.InvalidOperationException: Complex entry original ordinal '-1' is invalid for property
'...XDeepItem[]XInnerEntry.Inner' as it's outside of the collection of length '1'.

Root cause

InternalEntryBase.SetPropertyModified's recurse loop forced every flattened complex-collection element — including Added ones — to Modified/Unchanged. For a newly-grown element (an Added entry, whose original ordinal is legitimately -1), InternalComplexEntry.SetEntityState then ran ValidateOrdinal(original: true), which throws.

Changes

  • InternalEntryBase.SetPropertyModified: skip Added complex entries in the recurse loop, mirroring the guard already present in the bulk InternalComplexCollectionEntry.SetEntityState. Their state is left as computed by change detection.

    foreach (var complexEntry in GetFlattenedComplexEntries())
    {
        if (!UseOldBehavior38299
            && complexEntry.EntityState is EntityState.Added)
        {
            continue;
        }
    
        complexEntry.SetEntityState(newElementState, modifyProperties: true);
    }
  • 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_json to the relational ComplexCollectionJsonUpdateTestBase (new WidgetWithDeepJson model: 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.

Copilot AI and others added 3 commits June 6, 2026 02:43
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
Copilot AI requested a review from AndriySvyryd June 6, 2026 03:17
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.

ComplexProperty ToJson(): SaveChangesAsync throws 'ordinal -1 is invalid' when nested sub-collection grows (2+ JSON columns)

2 participants