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

Code before:
        : base(internalEntry)
    {
        _values = values;
        _complexCollectionValues = new List<ArrayPropertyValues?>?[ComplexCollectionProperties.Count];
        _nullComplexPropertyFlags = nullComplexPropertyFlags;
    }

    /// <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 ToObject()
    {
        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];
                    ((IRuntimeComplexProperty)complexProperty).GetSetter().SetClrValueUsingContainingEntity(structuralObject, 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 = ((IRuntimeComplexProperty)complexProperty).GetSetter().SetClrValue(structuralObject, list);

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


Review comment (suggestion):
@copilot This likely would be an issue if structuralObject is a value type contained in complex collection as it's only modifying a copy of structuralObject

Code after:
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public ArrayPropertyValues(InternalEntryBase internalEntry, object?[] values, bool[]? nullComplexPropertyFlags)
        : base(internalEntry)
    {
        _values = values;
        _complexCollectionValues = new List<ArrayPropertyValues?>?[ComplexCollectionProperties.Count];
        _nullComplexPropertyFlags = nullComplexPropertyFlags;
    }

    /// <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 ToObject()
    {
        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());