asp.net-mvc 将 datetime2 数据类型转换为 datetime 数据类型导致值超出范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1678474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:56:14  来源:igfitidea点击:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value

asp.net-mvcentity-frameworkdatetime

提问by Anthony Forloney

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 DateCreatedand DateModified. When I try to update the field, I want to keep DateCreatedtime the same, no reason to update the date it was created, and I change the DateModified time to the current time using DateTime.Now

我有一个 ASP.NET MVC 应用程序,我在其中编辑现有数据库以更新特定字段DateTime. 我的数据库有 4 个字段,其中两个是DateCreatedDateModified。当我尝试更新字段时,我想保持DateCreated时间相同,没有理由更新它的创建日期,我使用 DateModified 时间更改为当前时间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 Contextis 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.

这是给定的代码,以防万一我做错了。这是我第一次使用 ASP.NET MVC 所以要温和。我在Context被调用的地方看到了其他答案,但我找不到任何参考。当我运行该应用程序时,我在标题中收到错误消息,并且 显示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");
}

Please, any help is appreciated. Thanks.

请,任何帮助表示赞赏。谢谢。

采纳答案by Anthony Forloney

After reading this websiteI found to open the .edmx file for my database and change:

阅读本网站后,我发现打开我的数据库的 .edmx 文件并更改:

<...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?

这是可以接受的还是有更好的方法来修复该错误?

回答by Colin Asquith

For me, I had the same issue, but the problem was that by default when I saved my Model, an invalid DateTime was being created. I had a field CreatedOn and had not set it to anything, which meant the value was 01/01/0001 which was an invalid DateTime for SQL. Setting it got rid of the problem for me.

对我来说,我遇到了同样的问题,但问题是默认情况下,当我保存模型时,会创建一个无效的 DateTime。我有一个字段 CreatedOn 并且没有将其设置为任何内容,这意味着该值为 01/01/0001,这是 SQL 的无效日期时间。设置它为我解决了这个问题。

回答by Jamiegs

You can also edit the model (or in EF in the .edmx) and set the StoreGeneratedPatternto Computed for the field in question. That will cause it not to save that to that field, because it gets calculated by the SQL server.

您还可以编辑模型(或在 .edmx 中的 EF 中)并将StoreGeneratedPattern相关字段的设置为 Computed。这将导致它不会将其保存到该字段,因为它是由 SQL 服务器计算的。

回答by InterWAS

I have the same error and i discovered that if you have a SQL 2008 DB with a DateTime Nullable field, DON'T explicit assign Null (Nothing) to the field in your code, this will cause this error, or you can change all DateTime fields to DateTime2.

我有同样的错误,我发现如果你有一个带有 DateTime Nullable 字段的 SQL 2008 DB,不要显式地将 Null (Nothing) 分配给你代码中的字段,这会导致这个错误,或者你可以更改所有 DateTime字段到 DateTime2。

回答by Rushino

Just use a DateTime nullable such DateTime? in your domain entity property if your column in your database can be nullable. This will make it work.

只使用 DateTime 可以为空这样的 DateTime?如果您的数据库中的列可以为空,则在您的域实体属性中。这将使它起作用。