将datetime2数据类型转换为datetime数据类型会导致超出范围吗?

时间:2022-03-21 16:44:23

I am working on application contains a datepicker and if I set the time in that picker to a very old value or far in the future when I try to save this value in the database the server throw this exception, what is the cause of it?

我正在应用程序中包含一个datepicker,如果我将时间设置为一个非常旧的值或者将来当我试图在数据库中保存这个值时服务器抛出这个异常,它的原因是什么?

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.

将datetime2数据类型转换为datetime数据类型会导致超出范围的值。声明已被终止。

3 个解决方案

#1


63  

DateTime has the range: January 1, 1753, through December 31, 9999

DateTime的时间范围是:1753年1月1日到9999年12月31日

DateTime2 has the range: 0001-01-01 through 9999-12-31

DateTime2的范围是:0001-01到9999-12-31

So if you are entering a date before 1753 you would get this error when the field in the table is of type DateTime.

因此,如果您在1753年之前输入一个日期,那么当表中的字段类型为DateTime时,您将会得到这个错误。

#2


23  

Entity Framework will add default date for the field of value {01/01/0001 00:00:00} and this is outside the range of SQL Date Generation. So to make it work, we need to ask EF not to generate a default date, we can make it nullable by doing the following in the Model of that class.

实体框架将为值{01/01/0001 00:00}的字段添加默认日期,这超出了SQL日期生成的范围。为了让它工作,我们需要让EF不生成一个默认的日期,我们可以通过在这个类的模型中执行下列操作来使它为空。

In this case a default value is generated for the LastLoggedIn field by EntityFramework. If this field in the datebase can take null value, implies we can make it nullable by doing the follwoing in the model

在这种情况下,EntityFramework为LastLoggedIn字段生成一个默认值。如果datebase中的这个字段可以取空值,则意味着我们可以通过在模型中执行以下操作使其为空

 [Display(Name = "LastLoggedIn")]
        public DateTime? LastLoggedIn { get; set; }

#3


2  

Products prd = new Products();            
prd.CreatedDate = DateTime.Now;

You can add as above

您可以如上所述添加

#1


63  

DateTime has the range: January 1, 1753, through December 31, 9999

DateTime的时间范围是:1753年1月1日到9999年12月31日

DateTime2 has the range: 0001-01-01 through 9999-12-31

DateTime2的范围是:0001-01到9999-12-31

So if you are entering a date before 1753 you would get this error when the field in the table is of type DateTime.

因此,如果您在1753年之前输入一个日期,那么当表中的字段类型为DateTime时,您将会得到这个错误。

#2


23  

Entity Framework will add default date for the field of value {01/01/0001 00:00:00} and this is outside the range of SQL Date Generation. So to make it work, we need to ask EF not to generate a default date, we can make it nullable by doing the following in the Model of that class.

实体框架将为值{01/01/0001 00:00}的字段添加默认日期,这超出了SQL日期生成的范围。为了让它工作,我们需要让EF不生成一个默认的日期,我们可以通过在这个类的模型中执行下列操作来使它为空。

In this case a default value is generated for the LastLoggedIn field by EntityFramework. If this field in the datebase can take null value, implies we can make it nullable by doing the follwoing in the model

在这种情况下,EntityFramework为LastLoggedIn字段生成一个默认值。如果datebase中的这个字段可以取空值,则意味着我们可以通过在模型中执行以下操作使其为空

 [Display(Name = "LastLoggedIn")]
        public DateTime? LastLoggedIn { get; set; }

#3


2  

Products prd = new Products();            
prd.CreatedDate = DateTime.Now;

You can add as above

您可以如上所述添加