You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So, I got this surprisingly helpful warning message, telling me that my POCO property has a value converter but no value comparer. I had followed the guidance on this page and declared a value converter, but it took a lot of digging to come up with this SO answer that showed how to declare and wire up a value comparer.
The SO answer includes a bunch of undocumented magic, in particular propertyBuilder.Metadata.SetValueConverter and propertyBuilder.Metadata.SetValueComparer.
Since creation of a value converter apparently implies the necessity of creating a value comparer, please discuss this subject in this article.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
ID: 03dfe9f6-e831-ac3f-0b90-acb9b4c0f828
Version Independent ID: a26156d1-a4b3-7df0-e63e-b8e9f804abbf
Also shouldn't there be a default comparer for each primitive type? We have a custom string -> string converter but we still want a string comparer at the end of the day.
Edit: To add, the framework should realize this and use those default comparers instead of raising a warning.
where property is Microsoft.EntityFrameworkCore.Metadata.IMutableProperty and the .SetValueConverter(), .SetValueComparer() methods come from Microsoft.EntityFrameworkCore.MutablePropertyExtensions
I am hunting the internet for a good explanation of using the ValueConverter in my EF Core Db Context. Following is a snippet from my context. Is this snippet supposed to convert UTC from the database in to Local, and convert Local to UTC when SaveChanges() to the database?
protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){ValueConverter<DateTime,DateTime>dateTimeConverter=newValueConverter<DateTime,DateTime>(
v =>v.ToLocalTime(),
v =>DateTime.SpecifyKind(v,DateTimeKind.Utc));ValueConverter<DateTime?,DateTime?>nullableDateTimeConverter=newValueConverter<DateTime?,DateTime?>(
v =>v.HasValue?v.Value.ToLocalTime():v,
v =>v.HasValue?DateTime.SpecifyKind(v.Value,DateTimeKind.Utc):v);foreach(varentityTypeinmodelBuilder.Model.GetEntityTypes()){if(entityType.IsKeyless){continue;}foreach(varpropertyinentityType.GetProperties()){if(property.ClrType==typeof(DateTime)){property.SetValueConverter(dateTimeConverter);property.SetValueComparer(newValueComparer<DateTime>((l,r)=>l==r,
v =>StringComparer.Ordinal.GetHashCode(v),
v =>v));}elseif(property.ClrType==typeof(DateTime?)){property.SetValueConverter(nullableDateTimeConverter);property.SetValueComparer(newValueComparer<DateTime?>((l,r)=>l==r,
v =>StringComparer.Ordinal.GetHashCode(v),
v =>v));}}}
Yeah, I figured it out. The following code allows me to manually set and
store a date as UTC. When I read the date via the model, it comes back to
me as local time.
I wish there was a way for me NOT to have to manually set the time as UTC.
I wish the model would automatically convert it back to UTC when I save.
Anyway, that is what I wanted. The article you referenced, I'm sure it
explains this fully. But, I could not piece it together via that article.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
ValueConverter<DateTime, DateTime> dateTimeConverter = new
ValueConverter<DateTime, DateTime>(
v => v.ToUniversalTime(),
v => DateTime.SpecifyKind(v, DateTimeKind.Local));
ValueConverter<DateTime?, DateTime?> nullableDateTimeConverter = new
ValueConverter<DateTime?, DateTime?>(
v => v.HasValue ? v.Value.ToUniversalTime() : v,
v => v.HasValue ? DateTime.SpecifyKind(v.Value, DateTimeKind.Local) : v);
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
if (entityType.IsKeyless)
{
continue;
}
foreach (var property in entityType.GetProperties())
{
if (property.ClrType == typeof(DateTime))
{
property.SetValueConverter(dateTimeConverter);
property.SetValueComparer(new ValueComparer<DateTime>(
(l, r) => l == r,
v => StringComparer.Ordinal.GetHashCode(v),
v => v)
);
}
else if (property.ClrType == typeof(DateTime?))
{
property.SetValueConverter(nullableDateTimeConverter);
property.SetValueComparer(new ValueComparer<DateTime?>(
(l, r) => l == r,
v => StringComparer.Ordinal.GetHashCode(v),
v => v)
);
}
}
}
Activity
ajcvickers commentedon Dec 12, 2019
Part of #614ajcvickers commentedon Dec 12, 2019
Oops. That was for converters. Agree we need docs here.
SidShaytay commentedon Dec 16, 2019
Also shouldn't there be a default comparer for each primitive type? We have a custom string -> string converter but we still want a string comparer at the end of the day.
Edit: To add, the framework should realize this and use those default comparers instead of raising a warning.
ajcvickers commentedon Dec 16, 2019
@SidShetye There is. EF does use the default. But the default may not be correct.
dansoper commentedon Jan 9, 2020
Is this the same as #1680? That doesn't seem to be linked to this page, though.
ajcvickers commentedon Jan 15, 2020
See also #1680. Make sure this relates to collection conversions.
SidShaytay commentedon Jan 30, 2020
@ajcvickers till the documentation comes up, is this the correct approach for ValueComparers to avoid warnings with custom converters?
string
and
byte[]
where
propertyisMicrosoft.EntityFrameworkCore.Metadata.IMutablePropertyand the.SetValueConverter(),.SetValueComparer()methods come fromMicrosoft.EntityFrameworkCore.MutablePropertyExtensions7 remaining items
Documentation for value comparers
Documentation for value comparers
Documentation for value comparers
Documentation for value comparers
Documentation for value comparers
Documentation for value comparers (#2192)
ajcvickers commentedon Mar 20, 2020
Still need to update the fwlink.
JimInNC commentedon Apr 8, 2021
I am hunting the internet for a good explanation of using the ValueConverter in my EF Core Db Context. Following is a snippet from my context. Is this snippet supposed to convert UTC from the database in to Local, and convert Local to UTC when SaveChanges() to the database?
ajcvickers commentedon Apr 8, 2021
@JimInNC We have docs with many examples and sample code that can be downloaded from GitHub.
JimInNC commentedon Apr 8, 2021
weitzhandler commentedon Jul 4, 2025
Related issue: dotnet/efcore#17471 (comment)