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

[6.0.1] Allow non-nullable value comparer to be applied on nullable properties #26430

Merged
merged 1 commit into from
Nov 9, 2021
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
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