sqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
我的代码是这样的:
- using (var contxt = new realtydbEntities())
- {
- var status = GetStatus();
- var repIssue = new RepairIssue()
- {
- CreaterId = AuthorId,RepairItemDesc = this.txtDescription.Text,CreateDate = DateTime.Now,//here's the problem
- RepairIssueStatu = status
- };
- contxt.AddObject("RepairIssues",repIssue);
- contxt.SaveChanges();
- }
CreateDate属性映射到类型为smalldatetime的列.
如何让这段代码运行?
解决方法
您的问题的根源是C#DateTime对象比sql的smalldatetime类型“更大”.以下是对差异的概述:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
所以你的选择真的是:
>将列类型从smalldatetime更改为datetime(或datetime2)>而不是使用EF,构建自己的sql命令(并且可以使用sqlDateTime)