Skip to content

Commit

Permalink
Allow non-nullable value comparer to be applied on nullable properties
Browse files Browse the repository at this point in the history
Fixes #26429
  • Loading branch information
AndriySvyryd authored Nov 9, 2021
1 parent 32ca7e7 commit 539aee7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/EFCore/Metadata/Internal/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ public virtual CoreTypeMapping? TypeMapping
/// </summary>
public virtual string? CheckValueComparer(ValueComparer? comparer)
=> comparer != null
&& comparer.Type != ClrType
&& comparer.Type.UnwrapNullableType() != ClrType.UnwrapNullableType()
? CoreStrings.ComparerPropertyMismatch(
comparer.Type.ShortDisplayName(),
DeclaringEntityType.DisplayName(),
Expand Down
38 changes: 35 additions & 3 deletions test/EFCore.Tests/ModelBuilding/NonRelationshipTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ public virtual void Value_converter_configured_on_non_nullable_type_is_applied()
{
var modelBuilder = CreateModelBuilder(c =>
{
c.Properties<int>().HaveConversion<NumberToStringConverter<int>>();
c.Properties<int>().HaveConversion<NumberToStringConverter<int>, CustomValueComparer<int>>();
});

modelBuilder.Entity<Quarks>(
Expand All @@ -997,8 +997,40 @@ public virtual void Value_converter_configured_on_non_nullable_type_is_applied()
var model = modelBuilder.FinalizeModel();
var entityType = model.FindEntityType(typeof(Quarks));

Assert.IsType<NumberToStringConverter<int>>(entityType.FindProperty("Id").GetValueConverter());
Assert.IsType<NumberToStringConverter<int>>(entityType.FindProperty("Wierd").GetValueConverter());
var id = entityType.FindProperty("Id");
Assert.IsType<NumberToStringConverter<int>>(id.GetValueConverter());
Assert.IsType<CustomValueComparer<int>>(id.GetValueComparer());

var wierd = entityType.FindProperty("Wierd");
Assert.IsType<NumberToStringConverter<int>>(wierd.GetValueConverter());
Assert.IsType<CustomValueComparer<int>>(wierd.GetValueComparer());
}

[ConditionalFact]
public virtual void Value_converter_configured_on_nullable_type_overrides_non_nullable()
{
var modelBuilder = CreateModelBuilder(c =>
{
c.Properties<int?>().HaveConversion<NumberToStringConverter<int?>, CustomValueComparer<int?>>();
c.Properties<int>().HaveConversion<NumberToStringConverter<int>, CustomValueComparer<int>>();
});

modelBuilder.Entity<Quarks>(
b =>
{
b.Property<int?>("Wierd");
});

var model = modelBuilder.FinalizeModel();
var entityType = model.FindEntityType(typeof(Quarks));

var id = entityType.FindProperty("Id");
Assert.IsType<NumberToStringConverter<int>>(id.GetValueConverter());
Assert.IsType<CustomValueComparer<int>>(id.GetValueComparer());

var wierd = entityType.FindProperty("Wierd");
Assert.IsType<NumberToStringConverter<int?>>(wierd.GetValueConverter());
Assert.IsType<CustomValueComparer<int?>>(wierd.GetValueComparer());
}

[ConditionalFact]
Expand Down

0 comments on commit 539aee7

Please sign in to comment.