EF core 5.0 doesn't detect change to DateTimeOffset's offset
See original GitHub issueI 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 DateTimeOffset
s 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:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
@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: