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

Code before:
        public void HandleStateChange(InternalComplexEntry entry, EntityState oldState, EntityState newState)
        {
            Check.DebugAssert(oldState != newState, "State didn't change");

            var property = entry.ComplexProperty;
            Check.DebugAssert(property == _complexCollection, $"Expected {_complexCollection.Name}, got {property.Name}");
            if (oldState is EntityState.Detached)
            {
                if (newState is not EntityState.Deleted)
                {
                    InsertEntry(entry, original: false);
                }

                if (newState is not EntityState.Added)
                {
                    InsertEntry(entry, original: true);
                }
            }
            else if (oldState is EntityState.Deleted)
            {
                if (newState is not EntityState.Detached)
                {
                    InsertEntry(entry, original: false);
                }

                // When going from Deleted to Unchanged or Modified, restore the currentEntry to the original collection
                if (newState is EntityState.Unchanged or EntityState.Modified)
                {
                    InsertEntry(entry, original: true);
                }
            }
            else if (oldState == EntityState.Added
                     && newState is not EntityState.Detached)
            {
                InsertEntry(entry, original: true);
            }

            switch (newState)
            {
                case EntityState.Detached:
                    if (oldState is not EntityState.Deleted)
                    {
                        _entries?[entry.Ordinal] = null;
                    }

                    if (oldState is not EntityState.Added)
                    {
                        _originalEntries?[entry.OriginalOrdinal] = null;
                    }

                    var currentEntries = GetOrCreateEntries(original: false);

Review comment (suggestion):
@copilot Also change InternalComplexCollectionEntry.InsertEntry to just return when `entries[ordinal] == entry`

Code after:
        {
            var ordinal = original ? entry.OriginalOrdinal : entry.Ordinal;
            if (ordinal < 0 || ordinal >= entries.Count)
            {
                var property = entry.ComplexProperty;
                throw new InvalidOperationException(
                    original
                        ? CoreStrings.ComplexCollectionEntryOriginalOrdinalInvalid(
                            ordinal, property.ComplexType.ShortNameChain(), property.Name,
                            ((IList?)_containingEntry.GetOriginalValue(_complexCollection))?.Count ?? 0)
                        : CoreStrings.ComplexCollectionEntryOrdinalInvalid(
                            ordinal, property.ComplexType.ShortNameChain(), property.Name,
                            ((IList?)_containingEntry.GetCurrentValue(_complexCollection))?.Count ?? 0));
            }

            return ordinal;
        }

        public void HandleStateChange(InternalComplexEntry entry, EntityState oldState, EntityState newState)
        {
            Check.DebugAssert(oldState != newState, "State didn't change");

            var property = entry.ComplexProperty;
            Check.DebugAssert(property == _complexCollection, $"Expected {_complexCollection.Name}, got {property.Name}");
            if (oldState is EntityState.Detached)
            {
                if (newState is not EntityState.Deleted)
                {
                    InsertEntry(entry, original: false);
                }

                if (newState is not EntityState.Added)
                {
                    InsertEntry(entry, original: true);
                }
            }
            else if (oldState is EntityState.Deleted)
            {
                if (newState is not EntityState.Detached)
                {
                    InsertEntry(entry, original: false);
                }

                // When going from Deleted to Unchanged, restore the currentEntry to the original collection
                if (UseOldBehavior37724 && newState == EntityState.Unchanged)
                {
                    InsertEntry(entry, original: true);
                }
            }
            else if (oldState == EntityState.Added
                     && newState is not EntityState.Detached)