如何指定从EntityFramework检索的DateTime对象应该是DateTimeKind.UTC [英] How to specify that DateTime objects retrieved from EntityFramework should be DateTimeKind.UTC

查看:128
本文介绍了如何指定从EntityFramework检索的DateTime对象应该是DateTimeKind.UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有C#程序,其中所有的 DateTime 对象是 DateTimeKind.UTC 。将对象保存到数据库时,按预期方式存储UTC。但是,当他们收回他们时,他们是 DateTimeKind.Unspecified 。在C#中创建 DateTime 对象以始终使用 DateTimeKind.UTC 时,是否有方法告诉Entity Framework(Code First) ?

解决方案

不,没有。而实际上它是 DateTimeKind.Unspecified



但是,如果您担心支持多个时区,则应考虑使用 DateTimeOffset 。它就像一个常规的DateTime,除了它不代表时间的透视,它代表绝对视图,其中3PM(UTC - 3)等于4PM(UTC - 2)。 DateTimeOffset同时包含DateTime和时区,并由EntityFramework和SQL Server支持。


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?

解决方案

No, there's not. And it's actually DateTimeKind.Unspecified.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆