如何指定从检索到的EntityFramework datetime对象应该是DateTimeKind.UTC [英] How to specify that DateTime objects retrieved from EntityFramework should be DateTimeKind.UTC
问题描述
我的C#程序,所有的DateTime
对象 DateTimeKind.UTC
。当保存对象到数据库它存储UTC预期。然而,当retreiving他们,他们是 DateTimeKind.Unspecified
。有没有办法告诉实体框架(代码优先)当创建的DateTime
在C#中的对象总是使用 DateTimeKind.UTC
?
I have C# program where all DateTime
objects are DateTimeKind.UTC
. When saving the objects to the database it stores UTC as expected. However, when retreiving them, they are DateTimeKind.Unspecified
. Is there a way to tell Entity Framework (Code First) when creating DateTime
objects in C# to always use DateTimeKind.UTC
?
推荐答案
没有,有没有。它实际上是 DateTimeKind.Unspecified
。
No, there's not. And it's actually DateTimeKind.Unspecified
.
不过,如果你关心支持多个时区,你应该考虑使用的DateTimeOffset 。这就像一个普通的日期时间,但它并不代表一个时间的视角,它代表了一个绝对的图,其中下午3时(UTC - 3)等于下午4时(UTC - 2)。的DateTimeOffset包含日期时间和时区和它是由两个的EntityFramework和SQL Server支持
However, if you are concerned about supporting multiple timezones, you should consider using DateTimeOffset. It's like a regular DateTime, except that it does not represent a "perspective" of time, it represents an absolute view, in which 3PM (UTC - 3) equals 4PM (UTC - 2). DateTimeOffset contains both the DateTime and the time zone and it's supported by both EntityFramework and SQL Server.
这篇关于如何指定从检索到的EntityFramework datetime对象应该是DateTimeKind.UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!