Repository: dotnet/efcore
File: src/EFCore/ChangeTracking/Internal/InternalEntryBase.InternalComplexCollectionEntry.cs (C#)

Code before:
///     doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public partial class InternalEntryBase
{
    private struct InternalComplexCollectionEntry(InternalEntryBase entry, IComplexProperty complexCollection)
    {
        private List<InternalComplexEntry?>? _entries;
        private List<InternalComplexEntry?>? _originalEntries;
        private bool _isModified;
        private readonly InternalEntryBase _containingEntry = entry;
        private readonly IComplexProperty _complexCollection = complexCollection;

        public List<InternalComplexEntry?> GetOrCreateEntries(
            bool original,
            EntityState defaultState = EntityState.Detached)
        {
            IList? collection;
            int collectionCount;
            try
            {
                collection = original
                    ? (IList?)_containingEntry.GetOriginalValue(_complexCollection)
                    : (IList?)_containingEntry[_complexCollection];
                collectionCount = collection?.Count ?? 0;
            }
            catch (ArgumentOutOfRangeException)
            {
                // During state transitions, reading from the CLR collection can fail if the containing entry
                // is a complex entry with an out-of-bounds ordinal (e.g., when deleting from a nested complex collection).
                // In this case, use the existing entries list count which has already been initialized.
                collection = null;
                var existingEntries = original ? _originalEntries : _entries;
                collectionCount = existingEntries?.Count ?? 0;
            }

            var entries = EnsureCapacity(collectionCount, original, trim: false);
            if (collection != null
                && defaultState != EntityState.Detached
                && (defaultState != EntityState.Deleted || original)
                && (defaultState != EntityState.Added || !original))
            {
                for (var i = 0; i < entries.Count; i++)
                {
                    if (entries[i] != null)
                    {
                        continue;
                    }

                    var targetState = defaultState;
                    var newEntry = new InternalComplexEntry((IRuntimeComplexType)_complexCollection.ComplexType, _containingEntry, i);
                    if (original)

Review comment (suggestion):
@copilot Change the fix to avoid catching exceptions anywhere.
Add a quirk mode switch

Code after:
///     doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public partial class InternalEntryBase
{
    private struct InternalComplexCollectionEntry(InternalEntryBase entry, IComplexProperty complexCollection)
    {
        private static readonly bool UseOldBehavior37585 =
            AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue37585", out var enabled) && enabled;

        private List<InternalComplexEntry?>? _entries;
        private List<InternalComplexEntry?>? _originalEntries;
        private bool _isModified;
        private readonly InternalEntryBase _containingEntry = entry;
        private readonly IComplexProperty _complexCollection = complexCollection;

        public List<InternalComplexEntry?> GetOrCreateEntries(
            bool original,
            EntityState defaultState = EntityState.Detached)
        {
            var collection = GetCollection(original);
            var entries = EnsureCapacity(collection?.Count ?? 0, original, trim: false);
            if (collection != null
                && defaultState != EntityState.Detached
                && (defaultState != EntityState.Deleted || original)
                && (defaultState != EntityState.Added || !original))
            {
                for (var i = 0; i < entries.Count; i++)
                {
                    if (entries[i] != null)
                    {
                        continue;
                    }

                    var targetState = defaultState;
                    var newEntry = new InternalComplexEntry((IRuntimeComplexType)_complexCollection.ComplexType, _containingEntry, i);
                    if (original)
                    {
                        var otherEntries = _entries;
                        if (defaultState != EntityState.Deleted
                            && otherEntries != null)
                        {
                            if (otherEntries.Count <= i || otherEntries[i] != null)
                            {
                                var nullIndex = otherEntries.FindIndex(e => e == null);
                                if (nullIndex >= 0)
                                {
                                    newEntry.Ordinal = nullIndex;
                                }
                                else
                                {
                                    targetState = EntityState.Deleted;