Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert when using comparer for non-nullable dependent #30398

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/EFCore/ChangeTracking/Internal/SnapshotFactoryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ private Expression CreateSnapshotValueExpression(Expression expression, IPropert

if (comparer != null)
{
if (expression.Type != comparer.Type)
{
expression = Expression.Convert(expression, comparer.Type);
}

var snapshotExpression = ReplacingExpressionVisitor.Replace(
comparer.SnapshotExpression.Parameters.Single(),
expression,
Expand Down
12 changes: 9 additions & 3 deletions src/EFCore/Metadata/Internal/PropertyAccessorsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ private static PropertyAccessors CreateGeneric<TProperty>(IPropertyBase property

var shadowIndex = propertyBase.GetShadowIndex();
Expression currentValueExpression;
var propertyDefault = Expression.Constant(default(TProperty), typeof(TProperty));

if (shadowIndex >= 0)
{
currentValueExpression = Expression.Call(
Expand All @@ -72,7 +74,7 @@ private static PropertyAccessors CreateGeneric<TProperty>(IPropertyBase property
{
currentValueExpression = Expression.Condition(
currentValueExpression.MakeHasDefaultValue(propertyBase),
Expression.Constant(default(TProperty), typeof(TProperty)),
propertyDefault,
Expression.Convert(currentValueExpression, typeof(TProperty)));
}
}
Expand All @@ -83,14 +85,18 @@ private static PropertyAccessors CreateGeneric<TProperty>(IPropertyBase property
var comparer = (propertyBase as IProperty)?.GetValueComparer()
?? ValueComparer.CreateDefault(propertyBase.ClrType, favorStructuralComparisons: true);

var comparerDefault = comparer.Type != typeof(TProperty)
? (Expression)Expression.Convert(propertyDefault, comparer.Type)
: propertyDefault;

if (useStoreGeneratedValues)
{
currentValueExpression = Expression.Condition(
comparer.ExtractEqualsBody(
comparer.Type != currentValueExpression.Type
? Expression.Convert(currentValueExpression, comparer.Type)
: currentValueExpression,
Expression.Constant(default(TProperty), typeof(TProperty))),
comparerDefault),
Expression.Call(
entryParameter,
InternalEntityEntry.MakeReadStoreGeneratedValueMethod(typeof(TProperty)),
Expand All @@ -103,7 +109,7 @@ private static PropertyAccessors CreateGeneric<TProperty>(IPropertyBase property
comparer.Type != currentValueExpression.Type
? Expression.Convert(currentValueExpression, comparer.Type)
: currentValueExpression,
Expression.Constant(default(TProperty), typeof(TProperty))),
comparerDefault),
Expression.Call(
entryParameter,
InternalEntityEntry.MakeReadTemporaryValueMethod(typeof(TProperty)),
Expand Down
10 changes: 2 additions & 8 deletions src/EFCore/Metadata/RuntimeProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public class RuntimeProperty : RuntimePropertyBase, IProperty
private readonly PropertySaveBehavior _afterSaveBehavior;
private readonly Func<IProperty, IEntityType, ValueGenerator>? _valueGeneratorFactory;
private readonly ValueConverter? _valueConverter;
private readonly bool _explicitValueComparer;
private ValueComparer? _valueComparer;
private readonly bool _explicitKeyValueComparer;
private ValueComparer? _keyValueComparer;
private readonly ValueComparer? _providerValueComparer;
private CoreTypeMapping? _typeMapping;
Expand Down Expand Up @@ -99,9 +97,7 @@ public class RuntimeProperty : RuntimePropertyBase, IProperty

_typeMapping = typeMapping;
_valueComparer = valueComparer;
_explicitValueComparer = _valueComparer != null;
_keyValueComparer = keyValueComparer ?? valueComparer;
_explicitKeyValueComparer = keyValueComparer != null;
_providerValueComparer = providerValueComparer;
}

Expand Down Expand Up @@ -184,8 +180,7 @@ private ValueComparer GetKeyValueComparer()

private ValueComparer? GetValueComparer(HashSet<IReadOnlyProperty>? checkedProperties)
{
if (_explicitValueComparer // This condition is needed due to #28944
&& _valueComparer != null)
if (_valueComparer != null)
{
return _valueComparer;
}
Expand All @@ -211,8 +206,7 @@ private ValueComparer GetKeyValueComparer()

private ValueComparer? GetKeyValueComparer(HashSet<IReadOnlyProperty>? checkedProperties)
{
if (_explicitKeyValueComparer // This condition is needed due to #28944
&& _keyValueComparer != null)
if ( _keyValueComparer != null)
{
return _keyValueComparer;
}
Expand Down