将datetime2数据类型转换为日期时间数据类型会导致超出范围的值

时间:2022-11-02 16:43:03

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

我有一个ASP.NET MVC应用程序,我在其中编辑现有数据库以更新特定字段DateTime。我的数据库有4个字段,其中两个是DateCreated和DateModified。当我尝试更新字段时,我想保持DateCreated时间相同,没有理由更新它的创建日期,并且我使用DateTime.Now将DateModified时间更改为当前时间

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.

这是给定的代码,只是在我做错了什么的情况下。这是我第一次使用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.

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

5 个解决方案

#1


38  

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 DateTime。设置它摆脱了我的问题。

#2


19  

After reading this website I 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?

这是可接受的还是有更好的方法来解决这个错误?

#3


12  

You can also edit the model (or in EF in the .edmx) and set the StoreGeneratedPattern to 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 for the field。这将导致它不将其保存到该字段,因为它由SQL服务器计算。

#4


6  

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。

#5


3  

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?如果您的数据库中的列可以为空,则在您的域实体属性中。这将使它工作。

#1


38  

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 DateTime。设置它摆脱了我的问题。

#2


19  

After reading this website I 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?

这是可接受的还是有更好的方法来解决这个错误?

#3


12  

You can also edit the model (or in EF in the .edmx) and set the StoreGeneratedPattern to 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 for the field。这将导致它不将其保存到该字段,因为它由SQL服务器计算。

#4


6  

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。

#5


3  

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?如果您的数据库中的列可以为空,则在您的域实体属性中。这将使它工作。