'datetime2' error when using entity framework in VS 2010 .net 4.0
Entity framework handles all the dates as a Datetime2, so, if your fields in the database are Datetime, this could be a problem. We had the same problem here, and from what we found, populating all the date fields and changing the datatype, are the most commom solutions
If you are using Code First you must declare any optional DateTime
property as DateTime?
or Nullable<DateTime>
. Unset DateTime
objects can cause issues.
If the property is nullable in the database and a standard DateTime
in code (not DateTime?
), ADO.NET will send an insert command with a date of 0001-01-01 (not NULL
), but the minimum SQL DateTime value is 1753-01-01, causing an error. If your DateTime
property in the code is nullable (e.g. DateTime?
or Nullable<DateTime>
), the insert command is will attempt to insert a NULL
instead of an out-of-range date.