Repository: dotnet/efcore
File: src/EFCore/Query/ShapedQueryCompilingExpressionVisitor.cs (C#)

Code before:
                bool IsNullable,
                ParameterExpression MaterializationContextVariable,
                ParameterExpression ConcreteEntityTypeVariable,
                ParameterExpression ShadowValuesVariable) materializeExpressionContext)
        {
            var (returnType,
                nullable,
                materializationContextVariable,
                _,
                shadowValuesVariable) = materializeExpressionContext;

            var blockExpressions = new List<Expression>(2);

            var materializer = materializerSource
                .CreateMaterializeExpression(
                    new StructuralTypeMaterializerSourceParameters(
                        concreteStructuralType, "instance", returnType, nullable, queryTrackingBehavior), materializationContextVariable);

            if (_queryStateManager
                && concreteStructuralType is IRuntimeEntityType { ShadowPropertyCount: > 0 } runtimeEntityType)
            {
                var valueBufferExpression = Call(
                    materializationContextVariable, MaterializationContext.GetValueBufferMethod);

                // Include shadow properties from the entity and from nested non-collection complex types
                var shadowProperties = GetShadowPropertiesForMaterialization(runtimeEntityType)
                    .OrderBy(e => e.GetShadowIndex());

                if (shadowProperties.Any())
                {
                    blockExpressions.Add(
                        Assign(
                            shadowValuesVariable,
                            ShadowValuesFactoryFactory.Instance.CreateConstructorExpression(
                                runtimeEntityType,
                                NewArrayInit(
                                    typeof(object),
                                    shadowProperties.Select(
                                        p =>
                                            Convert(
                                                valueBufferExpression.CreateValueBufferReadValueExpression(
                                                    p.ClrType, p.GetIndex(), p), typeof(object)))))));
                }
            }

            materializer = materializer.Type == returnType
                ? materializer
                : Convert(materializer, returnType);
            blockExpressions.Add(materializer);

            return Block(blockExpressions);

Review comment (suggestion):
@copilot While this would work long term, in the current release EF doesn't support shadow property materialization on complex types. So, revert this change and instead, modify SnapshotFactoryFactory.CreateSnapshotExpression to special case shadow properties on complex types and skip them. And reference #35613 in a comment
Also make that conditional for when the following is false:
```
private static readonly bool UseOldBehavior37337 =
        AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue37337", out var enabled) && enabled;
```

Code after:
                bool IsNullable,
                ParameterExpression MaterializationContextVariable,
                ParameterExpression ConcreteEntityTypeVariable,
                ParameterExpression ShadowValuesVariable) materializeExpressionContext)
        {
            var (returnType,
                nullable,
                materializationContextVariable,
                _,
                shadowValuesVariable) = materializeExpressionContext;

            var blockExpressions = new List<Expression>(2);

            var materializer = materializerSource
                .CreateMaterializeExpression(
                    new StructuralTypeMaterializerSourceParameters(
                        concreteStructuralType, "instance", returnType, nullable, queryTrackingBehavior), materializationContextVariable);

            if (_queryStateManager
                && concreteStructuralType is IRuntimeEntityType { ShadowPropertyCount: > 0 } runtimeEntityType)
            {
                var valueBufferExpression = Call(
                    materializationContextVariable, MaterializationContext.GetValueBufferMethod);

                var shadowProperties = ((IEnumerable<IPropertyBase>)runtimeEntityType.GetProperties())
                    .Concat(runtimeEntityType.GetNavigations())
                    .Concat(runtimeEntityType.GetSkipNavigations())
                    .Where(n => n.IsShadowProperty())
                    .OrderBy(e => e.GetShadowIndex());

                // When we have a discriminator on a (table splitting) complex type (for the null/empty distinction), the entity type
                // has ShadowPropertyCount > 0 so end up here, but we don't actually support materializing shadow properties on
                // complex types (the LINQ just above doesn't include them, #35613).
                // So make sure there really are shadow properties to be materialized.
                if (shadowProperties.Any())
                {
                    blockExpressions.Add(
                        Assign(
                            shadowValuesVariable,
                            ShadowValuesFactoryFactory.Instance.CreateConstructorExpression(
                                runtimeEntityType,
                                NewArrayInit(
                                    typeof(object),
                                    shadowProperties.Select(
                                        p =>
                                            Convert(
                                                valueBufferExpression.CreateValueBufferReadValueExpression(
                                                    p.ClrType, p.GetIndex(), p), typeof(object)))))));
                }
            }