c# - Conversion of a datetime2 error using a nullable date column and EF 6.1.3 -


we using database first approach (sql server) , defined nullable date column. generated context in visual studio en confirmed [column(typename = "date")] added. when save new record (we confirmed value either null or has value > 01-01-2000) got following exception.

conversion of datetime2 data type datetime data type resulted in out-of-range value" exception.

we looked @ other questions , this article not find real reason error other changing database column nullable datetime2. known restriction of ef?

this happens when datetime field in database optional , value not set. 1 of entity-framework architectural things.

i recommend 1 debug issue, see if shows date 0001/1/1 , in case initializing field value or property nullable datetime value-type struct

as said (we confirmed value either null or between datetime.minvalue , datetime.maxvalue) solution 1 can find this:

var value = values[name]; if (value datetime) {     var date = (datetime)value;     if (date < sqldatetime.minvalue.value)     {         values[name] = sqldatetime.minvalue.value;     }     else if (date > sqldatetime.maxvalue.value)     {         values[name] = sqldatetime.maxvalue.value;     } } 

note sqldatetime.minvalue != datetime.minvalue

in entity framework set nullable property in edmx file true

enter image description here

hope helps in way :)


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo