Solve the datetime type field exception in Entity Framework.

Source: Internet
Author: User

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:

  1. Change the database field type to datetime2.
  2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.