Skip to content

Commit 2b855c1

Browse files
Copilotroji
andcommitted
Update fix to properly handle required complex types
Co-authored-by: roji <1862641+roji@users.noreply.github.com>
1 parent 39ae0a5 commit 2b855c1

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

src/EFCore/Query/Internal/StructuralTypeMaterializerSource.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ public Expression CreateMaterializeExpression(
121121
return structuralType is IComplexType complexType
122122
&& ReadComplexTypeDirectly(complexType)
123123
&& (UseOldBehavior37162 ? parameters.ClrType.IsNullableType() : parameters.IsNullable)
124-
&& complexType.ComplexProperty.IsNullable // Only apply null-checking wrapper if the complex property itself is nullable
125124
? HandleNullableComplexTypeMaterialization(
126125
complexType,
127126
parameters.ClrType,
@@ -132,13 +131,22 @@ public Expression CreateMaterializeExpression(
132131
// Creates a conditional expression that handles materialization of nullable complex types.
133132
// For nullable complex types, the method checks if all scalar properties are null
134133
// and returns default if they are, otherwise materializes the complex type instance.
134+
// For required complex types, always materializes the instance even if all properties are null.
135135
// If there's a required (non-nullable) property, only that property is checked for efficiency.
136136
Expression HandleNullableComplexTypeMaterialization(
137137
IComplexType complexType,
138138
Type clrType,
139139
Expression materializeExpression,
140140
MethodCallExpression getValueBufferExpression)
141141
{
142+
// If the complex property is required, don't apply null-checking wrapper
143+
if (!complexType.ComplexProperty.IsNullable)
144+
{
145+
return clrType.IsNullableType()
146+
? Convert(materializeExpression, clrType)
147+
: materializeExpression;
148+
}
149+
142150
// Get all scalar properties of the complex type (including nested ones).
143151
var allScalarProperties = complexType.GetFlattenedProperties().ToList();
144152

0 commit comments

Comments
 (0)