When I migrated a program using Entity Framework from MySQL to sqlserver today, an exception was reported during running: system. data. sqlclient. sqlexception: the conversion from the datetime2 data type to the datetime data type generates a value out of the range.
I found it online. The specific error cause is: The datetime type in C # is larger than the datetime type in SQL Server. The valid datetime range of sqlserver is January 1, January 1-9, 1753, December 31, 999. If the range is exceeded, EF converts datetime to datetime2, but the ing type in the database is still datetime, resulting in this exception.
That is to say, this exception has a precondition: the time range is outside 1753.1.1-9999.12.31. After knowing the cause, the solution is as follows:
- Change the database field type to datetime2.
- Maintain the time range of the program within 1753.1.1-9999.12.31.
Generally, the time range is not out of the datetime range of sqlserver. This exception occurs in my program because a time-type field is not initialized, the default time is 0001.1.1. If this field is not initialized, it is not correct. If this field is initialized, no exception is reported.
From: http://www.cnblogs.com/TianFang/archive/2013/05/20/3089289.html