DateTime.Kind comes back as Unspecified
See original GitHub issueIf you store a DateTime object to the DB with a DateTimeKind of either Utc or Local, when you read that record back from the DB you’ll get a DateTime object whose kind is Unspecified. Basically, it appears the Kind property isn’t persisted. This means that a test to compare equality of the before/after value of a round trip to the DB will fail.
Is there something that needs to be configured for the Kind to be preserved? Or what’s the recommended way to handle this?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:38
- Comments:87 (33 by maintainers)
Top Results From Across the Web
How to specify that DateTime objects retrieved from ...
I have C# program where all DateTime objects are DateTimeKind.UTC . When saving the objects to the database it stores UTC as expected....
Read more >DateTime.Kind Property (System)
Gets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither....
Read more >Fun with DateTime: DateTimeKind.Unspecified
This returns a new DateTime object that has the Kind set to "Local". And it has the value that we want as well:....
Read more >Specifying the Kind on DateTime
DateTimeKind is Unspecified. In other words, if the value passed in is Utc or Local, then you should honor it.
Read more >DateTime "Kind" changes to "Unspecified" after editing
Kind of the bound data gets changed to Unspecified. Is this behavior expected and is there a way for the modified value to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
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
A slight variation of this is that it would be nice if one could configure the default
DateTimeKind. For example, I know that all dates in my database are Utc, so it would be nice if this was specified for all fetched dates in the DbContext, or maybe configurable per entity property?Please note that EntityMaterializerSource is internal code and any solution like that shown above may get broken with a new release of EF. That doesn’t mean I’m saying not to use it–it’s publicly available internal code for a reason–but please be aware of the risk associated with it.
Starting in EF Core 2.1, this would be one way to deal with DateTime.Kind:
This will ensure every time the date is read from the database it is specified as Local automatically.