将datetime2数据类型转换为datetime数据类型会导致超出范围的值 [英] The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value
问题描述
DateTime
。我的数据库有4个字段,其中两个是 DateCreated 和 DateModified 。当我尝试更新该字段时,我希望保持 DateCreated 时间相同,没有理由更新创建日期,并且我将DateModified时间更改为当前时间使用 DateTime.Now
以下是给定的代码,以防万一我做错了。这是我第一次使用ASP.NET MVC这么温柔。我看到其他答案,其中上下文
被调用,但我找不到任何引用。当我运行应用程序时,我收到标题中的错误消息,并且 contractEntity.SaveChanges()
为红色。
public ActionResult Edit(Contract editContract){
var contract =(from c in contractEntity.Contracts where c.Id == editContract.Id select c).First();
if(!ModelState.IsValid)
return View(contract);
// editContract.DateCreated = contract.DateCreated;
// editContract.DateModified = DateTime.Now;
contractEntity.ApplyCurrentValues(contract.EntityKey.EntitySetName,editContract);
contractEntity.SaveChanges();
return RedirectToAction(Index);
}
请帮助,谢谢。谢谢。
阅读本网站我发现打开我的数据库的.edmx文件并更改:
< ... Provider =System.Data.SqlClientProviderManifestToken =2008... />
至
< ... Provider =System.Data.SqlClientProviderManifestToken =2005... />
这是可以接受还是修正错误的更好方法?
I have an ASP.NET MVC application where I am editing an existing database to update a paticular field, DateTime
. My database has 4 fields, two of which are DateCreated and DateModified. When I try to update the field, I want to keep DateCreated time the same, no reason to update the date it was created, and I change the DateModified time to the current time using DateTime.Now
Here is the given code just in-case I am doing something wrong. This is my first time using ASP.NET MVC so go gentle. I have seen other answers where Context
is called, but I can't find any reference to it. When I run the application I receive the error message in the title and the contractEntity.SaveChanges()
is in red.
public ActionResult Edit(Contract editContract) {
var contract = (from c in contractEntity.Contracts where c.Id == editContract.Id select c).First();
if (!ModelState.IsValid)
return View(contract);
// editContract.DateCreated = contract.DateCreated;
// editContract.DateModified = DateTime.Now;
contractEntity.ApplyCurrentValues(contract.EntityKey.EntitySetName, editContract);
contractEntity.SaveChanges();
return RedirectToAction("Index");
}
Please, any help is appreciated. Thanks.
After reading this website I found to open the .edmx file for my database and change:
<...Provider="System.Data.SqlClient" ProviderManifestToken="2008".../>
to
<...Provider="System.Data.SqlClient" ProviderManifestToken="2005".../>
Is this acceptable or is there a better approach to fixing that error?
这篇关于将datetime2数据类型转换为datetime数据类型会导致超出范围的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!