Skip to content

Commit cca7af7

Browse files
Don't invoke the type converter when the incoming complex type value is null
1 parent 0318067 commit cca7af7

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

src/PhenX.EntityFrameworkCore.BulkInsert/Metadata/PropertyAccessor.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ internal static class PropertyAccessor
2727
{
2828
// Validate the converter input type matches property type
2929
var converterParamType = converter.Parameters[0].Type;
30-
if (!converterParamType.IsAssignableFrom(propertyType) && !propertyType.IsAssignableFrom(converterParamType))
30+
if (!converterParamType.IsAssignableFrom(propertyType) &&
31+
!propertyType.IsAssignableFrom(converterParamType))
3132
{
32-
throw new ArgumentException($"Converter input must be assignable from property type ({propertyType} -> {converterParamType})");
33+
throw new ArgumentException(
34+
$"Converter input must be assignable from property type ({propertyType} -> {converterParamType})");
3335
}
3436

3537
// If property type != converter param, convert
@@ -39,7 +41,18 @@ internal static class PropertyAccessor
3941
converterInput = Expression.Convert(getterExpression, converterParamType);
4042
}
4143

42-
getterExpression = Expression.Invoke(converter, converterInput);
44+
var invokeConverter = Expression.Invoke(converter, converterInput);
45+
46+
if (propertyType.IsClass)
47+
{
48+
var nullCondition = Expression.Equal(getterExpression, Expression.Constant(null, propertyType));
49+
var nullResult = Expression.Constant(null, converter.ReturnType);
50+
getterExpression = Expression.Condition(nullCondition, nullResult, invokeConverter);
51+
}
52+
else
53+
{
54+
getterExpression = invokeConverter;
55+
}
4356

4457
propertyType = getterExpression.Type;
4558
}

0 commit comments

Comments
 (0)