将datetime2数据类型转换为datetime数据类型会导致超出范围的值 [英] The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value

查看:119
本文介绍了将datetime2数据类型转换为datetime数据类型会导致超出范围的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC应用程序,我正在编辑一个现有的数据库来更新一个关键字字段, 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆