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

Code before:
        if (_nullComplexPropertyFlags != null && NullableComplexProperties != null)
        {
            // Track which complex properties are being set to null to avoid trying to set nested properties
            var nullComplexTypes = new HashSet<IComplexType>();
            
            for (var i = 0; i < _nullComplexPropertyFlags.Length; i++)
            {
                if (_nullComplexPropertyFlags[i])
                {
                    var complexProperty = NullableComplexProperties[i];
                    
                    // Skip if this property is nested within a complex type that's already null
                    if (IsNestedInNullComplexType(complexProperty, nullComplexTypes))
                    {
                        continue;
                    }
                    
                    // Cast is safe: NullableComplexProperties is built from StructuralType.GetFlattenedComplexProperties()
                    // which returns runtime complex properties
                    ((IRuntimeComplexProperty)complexProperty).GetSetter().SetClrValueUsingContainingEntity(structuralObject, null);
                    nullComplexTypes.Add(complexProperty.ComplexType);
                }
            }
        }
        
        static bool IsNestedInNullComplexType(IComplexProperty complexProperty, HashSet<IComplexType> nullComplexTypes)
        {
            var declaringType = complexProperty.DeclaringType;
            while (declaringType is IComplexType complexType)
            {
                if (nullComplexTypes.Contains(complexType))
                {
                    return true;
                }
                declaringType = complexType.ComplexProperty.DeclaringType;
            }
            return false;
        }

        for (var i = 0; i < _complexCollectionValues.Length; i++)
        {
            var propertyValuesList = _complexCollectionValues[i];
            if (propertyValuesList == null)
            {
                continue;
            }

            var complexProperty = ComplexCollectionProperties[i];
            Check.DebugAssert(
                !complexProperty.IsShadowProperty(),
                $"Shadow complex property {complexProperty.Name} is not supported. Issue #31243");

Review comment (suggestion):
@copilot Instead of skipping properties nested in `null` complex properties here modify the `Clone` method to avoid marking properties nested in `null` complex properties as null.

Code after:
        var structuralObject = StructuralType.GetOrCreateMaterializer(MaterializerSource)(
            new MaterializationContext(new ValueBuffer(_values), InternalEntry.Context));

        if (_nullComplexPropertyFlags != null && NullableComplexProperties != null)
        {
            for (var i = 0; i < _nullComplexPropertyFlags.Length; i++)
            {
                if (_nullComplexPropertyFlags[i])
                {
                    var complexProperty = NullableComplexProperties[i];
                    structuralObject = SetNestedComplexPropertyValue(structuralObject, complexProperty, null);
                }
            }
        }

        for (var i = 0; i < _complexCollectionValues.Length; i++)
        {
            var propertyValuesList = _complexCollectionValues[i];
            if (propertyValuesList == null)
            {
                continue;
            }

            var complexProperty = ComplexCollectionProperties[i];
            Check.DebugAssert(
                !complexProperty.IsShadowProperty(),
                $"Shadow complex property {complexProperty.Name} is not supported. Issue #31243");
            var list = (IList)((IRuntimeComplexProperty)complexProperty).GetIndexedCollectionAccessor().Create(propertyValuesList.Count);
            structuralObject = SetNestedComplexPropertyValue(structuralObject, complexProperty, list);

            foreach (var propertyValues in propertyValuesList)
            {
                list.Add(propertyValues?.ToObject() ?? complexProperty.ComplexType.ClrType.GetDefaultValue());
            }
        }

        return structuralObject;
    }

    private object SetNestedComplexPropertyValue(object structuralObject, IComplexProperty complexProperty, object? value)
    {
        return UseOldBehavior37516
            ? ((IRuntimeComplexProperty)complexProperty).GetSetter().SetClrValue(structuralObject, value)
            : SetValueRecursively(structuralObject, complexProperty.GetChainToComplexProperty(fromEntity: false), 0, value);

        static object SetValueRecursively(object instance, IReadOnlyList<IComplexProperty> chain, int index, object? value)
        {
            var currentProperty = (IRuntimeComplexProperty)chain[index];
            if (index == chain.Count - 1)
            {
                return currentProperty.GetSetter().SetClrValue(instance, value);