Skip to main content
 首页 » 编程设计

c#之DateTime 到 Datetime2

2024年02月13日11mfryf

我已经开始使用 Entity Framework ,经过研究我发现 Entity Framework 只接受 DateTime 格式 DateTime2。

将 DateTime 选择器转换为 Datetime2 格式的最佳方法是什么

我目前正在做以下事情

string strCastFrom = this.dtCastFrom.Value.Year.ToString("0000") + "-" + this.dtCastFrom.Value.Month.ToString("00") + "-" + this.dtCastFrom.Value.Day.ToString("00") + "T00:00:00"; 

是否有更好的方法将 datetime 转换为 DateTime2?

请您参考如下方法:

不要执行任何 字符串转换。只需使用

DateTime value = dtCastFrom.Value; 

让 Entity Framework 自己负责将数据获取到数据库。您不想要字符串表示,也没有理由通过字符串表示。这不仅仅是 EF 的情况,这是一个一般原则。