[英]DateTime2 error when I'm not using DateTime2
在控制器上运行我的create动作时出现此错误:datetime2数据类型到datetime数据类型的转换导致值超出范围”
这是我的Domain类:
public class MyEntity
{
public Guid Id {get; set;}
public string Name {get; set;}
public DateTime DateAdded {get; set;}
}
在控制器“ POST”中创建操作,我这样做:
[HttpPost]
public ActionResult Create(MyEntity entity)
{
entity.Id = Guid.NewGuid();
entity.DateAdded = DateTime.Now;
// the name will be added from the view
db.MyContext.Add(entity);
db.Save();
}
我不确定datatime2的来源。
在对SQL Server数据库建模时,实体框架考虑类型为DateTime2
所有日期字段。
因此,如果在您的SQL Server数据库中,实际上将一个字段定义为DateTime
,则当您将日期留空或为DateTime2
指定范围内但超出范围的另一个值时,将出现范围外问题。 DateTime
。 实体框架本身并没有解决此问题,因为它仅假设DateTime2
。
解决方案是检查所有日期字段的值是否已填写,并且它们是否在范围内。
我修好了 我忘记/忽略了我有一个“ DateChange”字段
现在可以使用
public class MyEntity
{
public Guid Id {get; set;}
public string Name {get; set;}
public DateTime DateAdded {get; set;}
public DateTime DateChanged {get; set;}
}
在控制器“ POST”中创建操作,我这样做:
[HttpPost]
public ActionResult Create(MyEntity entity)
{
entity.Id = Guid.NewGuid();
entity.DateAdded = DateTime.Now;
entity.DateChanged = DateTime.Now;
// the name will be added from the view
db.MyContext.Add(entity);
db.Save();
}
仍然不确定为什么会给它转换datetime2错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.