简体   繁体   English

DateTime至Datetime2

[英]DateTime to Datetime2

I have started to use entity framework and after doing research i have found that Entity Framework only accepts the DateTime format DateTime2. 我已经开始使用实体框架,经过研究,我发现实体框架仅接受DateTime格式的DateTime2。

What is the best way to convert a DateTime picker to the format of Datetime2 将DateTime选择器转换为Datetime2格式的最佳方法是什么

I currently do the following 我目前正在执行以下操作

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

Is therea better way to convert datetime to DateTime2? 有没有更好的方法将datetime转换为DateTime2?

Don't perform any string conversions. 不要执行任何字符串转换。 Just use 只需使用

DateTime value = dtCastFrom.Value;

Let the Entity Framework itself take care of getting the data to the database. 让实体框架本身负责将数据获取到数据库。 You don't want a string representation, and there's no reason to go via a string representation. 不想字符串表示,并没有任何理由通过字符串表示去。 This isn't just the case for EF, it's a general principle. 这不仅仅是EF的情况,这是一个普遍原则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM