Created
January 7, 2022 15:09
-
-
Save marcominerva/511b3263b2ee1d6b70c8b64cfbfd5523 to your computer and use it in GitHub Desktop.
DateOnly Converter & Comparer for Entity Framework Core 6.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DateOnlyConverter : ValueConverter<DateOnly, DateTime> | |
{ | |
public DateOnlyConverter() : base( | |
dateOnly => dateOnly.ToDateTime(TimeOnly.MinValue), | |
dateTime => DateOnly.FromDateTime(dateTime)) | |
{ | |
} | |
} | |
public class DateOnlyComparer : ValueComparer<DateOnly> | |
{ | |
public DateOnlyComparer() : base( | |
(d1, d2) => d1.DayNumber == d2.DayNumber, | |
d => d.GetHashCode()) | |
{ | |
} | |
} |
Yes, ValueCompararer
is used for the ChangeTracker
, as you can read here: https://docs.microsoft.com/en-us/ef/core/modeling/value-comparers?tabs=ef5
That's a quick response. Thank you.
I'm reading it right now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello.
What is the
ValueComparer
for?I read there is a problem with change tracker when only the
DateOnly
typed property is changed.Has it anything to do with it?
Thanks.