0
votes

Have read a number of posts about this issue but haven't found anything that works.

Our SQL database has the field set up like so (Data Type of datetime)

enter image description here

The respective model property has the following declarations

    [DisplayName("Effective Date")]
    [Column(TypeName = "DateTime")]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime? EffectiveDate { get; set; }

Yet if I enter a date of 11/11/1111 I get the following error when my program runs DbContext.SaveChanges()

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

I am not aware of, nor have found, any range specifications in our ASP MVC project or on the SQL table. Any help greatly appreciated.

2

2 Answers

5
votes

You have specified column type as:

[Column(TypeName = "DateTime")]

This will map to Dateime type column in SQL server, and not Datetime2.

DateTime has a valid range of January 1, 1753, through December 31, 9999 so your value 11/11/1111 is out of range.

The error message includes datetime2 and that is due the the provided value 11/11/1111, which would fall under datetime2 range.

2
votes

The DateTime data type's range for MSSQL is January 1, 1753, through December 31, 9999

http://msdn.microsoft.com/en-us/library/ms187819.aspx

The error message stating the use of datetime2 is a bit misleading, but ultimately the issue is that you're trying to use a date that is out of the range of the DateTime data type.