EF 6.0 The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. c#中的时间

在SQL server 中,有两种日期时间类型:

1、DateTime 它的范围是 1753-1-1 至 9999-12-31

2、DateTime2 它的满园是 0001-01-01 至 9999-12-31

EF实体框架给一个DateTime字段加载一个默认值是 {01/01/0001 00:00:00},它已经在SQL日期类型的范围之外了。所以,如果要让他正常工作,我们需要告诉EF框架不需要创建一个默认的日期时间值。我们可以在模型类型上加一个可空类型,表示它的值是可以为空的。

 [Display(Name = "LastLoggedIn")]
 public DateTime? LastLoggedIn { get; set; }


日期时间对象创建:

BirthDate = new DateTime(1990, 6, 1),
DateTime.Parse("2010/10/1");

转换日期:
分为长日期,长时间和短日期和短时间。
BirthDate.ToString("D") 转换为本地化的时间,如1990年6月1日。
Birthdate.ToLongDateString()
BirthDate.ToString("d") 转换为简写的时间。如1990/6/1.
BithDate.ToShortDateString();

BithDate.ToString("yyyy-MM-dd")  1990-06-01

 

 

 

 

 

 









posted on 2015-03-28 12:15  众里寻他千万度  阅读(1401)  评论(0编辑  收藏  举报

导航