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

Code before:
            : base.ReadOriginalValue<T>(property, originalValueIndex);

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public override object? GetOriginalValue(IPropertyBase propertyBase)
        => EntityState == EntityState.Added
            ? GetCurrentValue(propertyBase)
            : base.GetOriginalValue(propertyBase);

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    protected override void SetEntityState(EntityState oldState, EntityState newState, bool acceptChanges, bool modifyProperties)
    {
        if (oldState is EntityState.Detached or EntityState.Deleted
            && newState is not EntityState.Detached and not EntityState.Deleted)
        {
            if (!UseOldBehavior37724 && oldState is EntityState.Deleted && Ordinal == -1)
            {
                Ordinal = OriginalOrdinal;
            }

            ContainingEntry.ValidateOrdinal(this, original: false);
        }

        if (oldState is EntityState.Detached or EntityState.Added
            && newState is not EntityState.Detached and not EntityState.Added)
        {
            ContainingEntry.ValidateOrdinal(this, original: true);
        }

        if ((oldState is EntityState.Deleted && newState is EntityState.Added)
            || (oldState is EntityState.Added && newState is EntityState.Deleted))
        {
            throw new InvalidOperationException(CoreStrings.ComplexCollectionEntryInvalidStateChange(ComplexProperty.Name));
        }

        base.SetEntityState(oldState, newState, acceptChanges, modifyProperties);
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that

Review comment (suggestion):
@copilot Also modify `HandleStateChange` in `InternalComplexCollectionEntry` to restore the currentEntry to the original collection when newState is EntityState.Modified

Code after:
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public override object? GetOriginalValue(IPropertyBase propertyBase)
        => EntityState == EntityState.Added
            ? GetCurrentValue(propertyBase)
            : base.GetOriginalValue(propertyBase);

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    protected override void SetEntityState(EntityState oldState, EntityState newState, bool acceptChanges, bool modifyProperties)
    {
        if (oldState is EntityState.Detached or EntityState.Deleted
            && newState is not EntityState.Detached and not EntityState.Deleted)
        {
            if (!UseOldBehavior37724 && Ordinal == -1)
            {
                Ordinal = OriginalOrdinal;
            }

            ContainingEntry.ValidateOrdinal(this, original: false);
        }

        if (oldState is EntityState.Detached or EntityState.Added
            && newState is not EntityState.Detached and not EntityState.Added)
        {
            ContainingEntry.ValidateOrdinal(this, original: true);
        }

        if ((oldState is EntityState.Deleted && newState is EntityState.Added)
            || (oldState is EntityState.Added && newState is EntityState.Deleted))
        {
            throw new InvalidOperationException(CoreStrings.ComplexCollectionEntryInvalidStateChange(ComplexProperty.Name));
        }

        base.SetEntityState(oldState, newState, acceptChanges, modifyProperties);
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    protected override void OnStateChanging(EntityState newState)