Skip to content

Commit

Permalink
Apply pre-convention value converters on nullable types
Browse files Browse the repository at this point in the history
Fixes #25896
  • Loading branch information
AndriySvyryd authored Sep 10, 2021
1 parent 9f863be commit 243bce0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/EFCore/Metadata/Internal/PropertyConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ public virtual void Apply(IMutableProperty property)

break;
case CoreAnnotationNames.ValueConverterType:
if (ClrType == property.ClrType)
if (ClrType.UnwrapNullableType() == property.ClrType.UnwrapNullableType())
{
property.SetValueConverter((Type?)annotation.Value);
}

break;
case CoreAnnotationNames.ValueComparerType:
if (ClrType == property.ClrType)
if (ClrType.UnwrapNullableType() == property.ClrType.UnwrapNullableType())
{
property.SetValueComparer((Type?)annotation.Value);
}
Expand Down
21 changes: 21 additions & 0 deletions test/EFCore.Tests/ModelBuilding/NonRelationshipTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,27 @@ public virtual void Properties_can_have_value_converter_configured_by_type()
Assert.IsType<ValueComparer<WrappedString>>(wrappedProperty.GetValueComparer());
}

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

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

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());
}

[ConditionalFact]
public virtual void Value_converter_configured_on_base_type_is_not_applied()
{
Expand Down

0 comments on commit 243bce0

Please sign in to comment.