[release/10.0] Fix ArgumentOutOfRangeException when deleting from a complex collection with nested collections#37702
Conversation
src/EFCore/ChangeTracking/Internal/InternalEntryBase.InternalComplexCollectionEntry.cs
Outdated
Show resolved
Hide resolved
|
@copilot Several tests are failing. Make sure to run all tests after fixing these: |
... Fixed in 704c6a1. The issue was that SetState is called during change detection (not just after AcceptChanges), so we need to read from the CLR collection to detect size changes. The fix now uses exception handling as a fallback only when ordinals are out of bounds, preserving normal change detection behavior. |
|
@copilot Update progress by rewriting the PR summary using this template: |
... Updated the PR description with the requested template format. |
src/EFCore/ChangeTracking/Internal/InternalEntryBase.InternalComplexCollectionEntry.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Relational.Specification.Tests/Update/ComplexCollectionJsonUpdateTestBase.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Relational.Specification.Tests/Update/ComplexCollectionJsonUpdateTestBase.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull request overview
This pull request fixes an ArgumentOutOfRangeException that occurs when deleting an item from a complex collection that contains nested complex collections. The issue (#37585) affects EF Core 10.0.x where complex collections are a new feature. While the database update succeeds, the exception prevents normal completion of SaveChanges().
Changes:
- Added a
GetCollectionmethod that safely handles accessing nested collections when the parent complex entry is in a deleted or added state (ordinal = -1) - Introduced a quirk mode switch
Microsoft.EntityFrameworkCore.Issue37585to allow reverting to the old behavior if needed - Added comprehensive test coverage for removal from complex collections with nested collections across multiple entity types (classes, structs, records, fields)
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/EFCore/ChangeTracking/Internal/InternalEntryBase.InternalComplexCollectionEntry.cs | Core fix: Added GetCollection method that returns null when parent complex entry has invalid ordinal (-1), preventing ArgumentOutOfRangeException when accessing nested collections. Added quirk mode switch. |
| test/EFCore.Specification.Tests/ComplexTypesTrackingTestBase.cs | Added test methods for removing items from complex collections with nested collections, covering 8 different entity type variations (class, struct, readonly struct, record, field-based variants) |
| test/EFCore.SqlServer.FunctionalTests/ComplexTypesTrackingSqlServerTest.cs | Overrode new test methods with empty bodies due to Issue #36175 (notification change tracking not supported) and field proxying limitations |
| test/EFCore.Relational.Specification.Tests/Update/ComplexCollectionJsonUpdateTestBase.cs | Minor formatting change: converted expression-bodied method to block body for consistency |
artl93
left a comment
There was a problem hiding this comment.
New feature, significant customer feedback. Approved.
test/EFCore.Relational.Specification.Tests/Update/ComplexCollectionJsonUpdateTestBase.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Relational.Specification.Tests/Update/ComplexCollectionJsonUpdateTestBase.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Relational.Specification.Tests/Update/ComplexCollectionJsonUpdateTestBase.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Relational.Specification.Tests/Update/ComplexCollectionJsonUpdateTestBase.cs
Outdated
Show resolved
Hide resolved
…with nested arrays Fixes #37585 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
3a5f39a to
1ad6f04
Compare
Fixes #37585
Description
When deleting an item from a complex collection that contains nested complex collections, an
ArgumentOutOfRangeExceptionis thrown duringSaveChanges. The database update succeeds, but the exception prevents normal completion of the operation.Customer impact
An exception is thrown in the affected scenarios; there is no good workaround. Models with nested complex collections are relatively common for apps mapping to JSON columns.
How found
7 customer reports on 10.0.x
Regression
No, complex collections are a new feature in EF 10
Testing
Tests added
Risk
Low. The changes only affect nested complex collections. Quirk mode added.
Original prompt
This section details on the original issue you should resolve
<issue_title>Deleting an item from a ComplexCollection, that contains an array results in Error</issue_title>
<issue_description>### Bug description
This may be resolved with the same fix as #37377; however, posting to be sure.
When deleting an item from a ComplexCollection, that Contains an array, an error is thrown.
Using the code below, calling SaveChanges(), results in an exception. What is unusual, is the save still goes through and the database result is correct.
Your code