33

I have a table w/ a sql date data type. When I look at the EDM markup, the storage element reflects this. The conceptual entity has a data type of DateTime (there doesn't appear to be a Date data type). When I call save changes and have a DateTime instance associated w/ the entity I want to persist, I get the following error:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.

How can I use the date data type in my schema and have EF behave? Thanks!

1 Answer 1

49

Sounds like your SQL date column is non-nullable, but you haven't initialized the date on your entity before saving?

Sign up to request clarification or add additional context in comments.

5 Comments

Yeah I just realized this had nothing to do w/ my date column, I have another DateTime column that has a default specified within the db but I forgot to change the the StoreGeneratedPattern property in the SSDL to get this working. My bad.
@Pop Catalin: Hard to blame the EF here; it's behaving correctly. SQL Server just has a really weirdly limited DateTime range.
I meant in general, this was just one of the many issues I've encountered in a very short time period ... (And BTW, EF came after SQL Server, I've expected more from EF)
Well, I was having this same problem tonight, and magically somehow I found my way to this page as a top result to my google search and thanks to this answer, realized i just needed to make my domain (POCO) model (via EF code-first) Nullable<DateTime> instead of DateTime and now things work! Yay!
The valid range for SQL Server's DATETIME type is January 1, 1753, through December 31, 9999. If EF detects that the value is outside this range, it automatically tries to use DATETIME2 instead of DATETIME to store the value. To avoid this unwanted behavior, you have to add code in your application to make certain the value is within the valid DATETIME range. Also see stackoverflow.com/questions/3586566/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.