C# 如何修复“SqlException:将 datetime2 数据类型转换为 datetime 数据类型导致值超出范围。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10495557/
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-08-09 14:03:20  来源:igfitidea点击:

How to fix "SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value."

c#asp.netentity-frameworkwebforms

提问by u1230329

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

SqlException:将 datetime2 数据类型转换为 datetime 数据类型导致值超出范围。

my code is like this:

我的代码是这样的:

        using (var contxt = new realtydbEntities())
        {
            var status = GetStatus();

            var repIssue = new RepairIssue()
            {
                CreaterId = AuthorId,
                RepairItemDesc = this.txtDescription.Text,
                CreateDate = DateTime.Now,//here's the problem
                RepairIssueStatu = status
            };

            contxt.AddObject("RepairIssues", repIssue);
            contxt.SaveChanges();
        }

the CreateDateproperty mapping to a column which type is smalldatetime.

所述CREATEDATE属性映射到列哪种类型SMALLDATETIME

how to make this code run?

如何让这段代码运行?

采纳答案by Jason

The root of your problem is that the C# DateTime object is "bigger" than SQL's smalldatetime type. Here's a good overview of the differences: http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes

问题的根源在于 C# DateTime 对象比 SQL 的 smalldatetime 类型“大”。这是差异的一个很好的概述:http: //karaszi.com/the-ultimate-guide-to-the-datetime-datatypes

So really your options are:

所以你的选择是:

  1. Change the column type from smalldatetime to datetime (or datetime2)
  2. Instead of using EF, construct your own SQL Command (and you can use SqlDateTime)
  1. 将列类型从 smalldatetime 更改为 datetime(或 datetime2)
  2. 不使用 EF,而是构建您自己的 SQL 命令(并且您可以使用 SqlDateTime)

回答by Rob

SqlDateTimewill allow you to do what you need.

SqlDateTime将允许您执行所需的操作。

回答by Milton

I had the same exception, but it was because a non nullable datetime property that taking the min datetime value. That wasn't a smalldatetime at DB, but the min datetime of C# exceed the limit of min datetime of SQL. The solution was obvious, set the datetime properly. BTW, the code wasn't mine, and that's why I wasn't aware of that property :)

我有同样的例外,但这是因为一个不可为空的日期时间属性采用最小日期时间值。那不是 DB 的 smalldatetime,但是 C# 的 min datetime 超过了 SQL 的 min datetime 的限制。解决方案很明显,正确设置日期时间。顺便说一句,代码不是我的,这就是我不知道该属性的原因:)

回答by midohioboarder

I got this error because I had added a datetime column to my SQL table and application WITHOUT removing the old data. I found that I could update new records; but, the records that were in the table prior to the added field threw this error when an update was attempted on one of them.

我收到此错误是因为我在没有删除旧数据的情况下向 SQL 表和应用程序添加了日期时间列。我发现我可以更新新记录;但是,当尝试更新其中一个字段时,表中添加字段之前的记录会引发此错误。

回答by Howie Krauth

Add this to your model class:

将此添加到您的模型类:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Properties<DateTime>().Configure(c => c.HasColumnType("datetime2"));
    }

回答by Ghadir Farzaneh

check your migration content. I changed my model like this"

检查您的迁移内容。我像这样改变了我的模型”

public DateTime? CreationDate { get; set; }

公共日期时间?创建日期 { 获取;放; }

then add new migration and finally run update database

然后添加新的迁移,最后运行更新数据库