Skip to content

Instantly share code, notes, and snippets.

@marcominerva
Created January 7, 2022 15:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcominerva/511b3263b2ee1d6b70c8b64cfbfd5523 to your computer and use it in GitHub Desktop.
Save marcominerva/511b3263b2ee1d6b70c8b64cfbfd5523 to your computer and use it in GitHub Desktop.
DateOnly Converter & Comparer for Entity Framework Core 6.0
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())
{
}
}
@EminoMeneko
Copy link

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.

@marcominerva
Copy link
Author

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

@EminoMeneko
Copy link

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