question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EF core 5.0 doesn't detect change to DateTimeOffset's offset

See original GitHub issue

I see the following bug with EF 5.0

var entity = dbCtx.Find<MyEntity>(123);//Load from DB context
entity.MyDateTimeOffset = entity.MyDateTimeOffset.ToOffset(entity.MyDateTimeOffset.Add(TimeSpan.FromHours(2)));//Switch the offset by 2 hours
ctx.Entry(order).State; //Return Unchanged but expecting Modified

I believe this happens because IEquatable<DateTimeOffset> considers that two DateTimeOffsets are equal if they represent the same absolute time. However, from a DB perspective, the code above should detect the change and persist the new offset in the column (which is of type datetimeoffset in Sql Azure)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
rojicommented, Apr 6, 2021

I can confirm this repros - and as @clement911 wrote this is due to the equality behavior .NET defines for DateTimeOffset, which doesn’t take timezone differences into account.

1reaction
rojicommented, Apr 7, 2021

@clement911 I’ve submitted a fix for this. In the meantime you can set up your own value comparer with the right behavior as described in the docs:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Blog>().Property(b => b.DateTimeOffset)
        .Metadata
        .SetValueComparer(new ValueComparer<DateTimeOffset>(
            (dto1, dto2) => dto1 == dto2 && dto1.Offset == dto2.Offset,
            dto => dto.GetHashCode()));
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

EF Core query only DateTime of DateTimeOffset cannot be ...
I'm having an issue where I want to query based on the DateTime only of a DateTimeOffset SQL Server column and I'm wondering...
Read more >
Regarding the "It is no longer possible to write ...
Just started upgrading from .NET 5 to .NET 6, including NPGSQL EF Core 6. All went smoothly for the most part till I...
Read more >
A Warning For EF Core's DateTimeOffsetToBinaryConverter
I recently had some troubles sorting and filtering a DateTimeOffsetToBinaryConverter column in a SQLite database using ef-core.
Read more >
Converting between DateTime and DateTimeOffset
DateTimeOffset reflects a time's offset from UTC, but it doesn't reflect the actual time zone to which that offset belongs.
Read more >
Why Use DateTimeOffset | Blog
If you're using EF Core and Migrations, they support DateTimeOffset just fine, so no need to avoid using it because of issues there....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found