Conversions from the DATETIME2 data type to the datetime data type produce an out-of-range value

Source: Internet
Author: User

When I execute a program written in EF 6.0, the error is as follows: Conversions from the datetime2 data type to the datetime data type produce an out-of-range value.

Development environment:

Visual Studio 2013

. NET Framework 4.5

Entity Framework 6.0


After online collection, white pillow in his blog to write the detailed reasons:

SQLSERVER2008 provides datetime2 types of data (more accurate than datetime), and entity framework4 defaults to C # The System.DateTime class in the sqlServer2008 is converted to the DateTime2 type in the system, and the attributes of the elements in your database are set to the DateTime type, and converting datetime2 to a DateTime will be considered as losing data, so it will be an error.

There are two types of solutions available on-line: 1, explicit assignment, 2. Change database field to datetime2 type. Just think that these two solutions have relatively large limitations, so did some attempts to find some ways to use, specifically as follows:

1. If your datetime Type field is not assigned a value when added, set the "nullable" property of the field to true in the Model browser;

2. If you have a DateTime type field and you need to assign a value, set the value explicitly instead of having the database script set the default value;

3, update the same as the new treatment can be;

Conversions from the DATETIME2 data type to the datetime data type produce an out-of-range value

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.