Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
736 views
in Technique[技术] by (71.8m points)

c# - Error - SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM

i have an app written for 2008.

We are using linq to entities.

We've now had to switch the DB to 2005. I am getting the following error on linq SELECT queries:

Error - SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

The offending line is:

DateOfBirth = ((s.Date_Of_Birth == null) || (s.Date_Of_Birth <= lowdate)) ?
    DateTime.MinValue : s.Date_Of_Birth.Value,

DateOfBirth is of type DateTime and a property in our own business object (not entity).

Anyone know how i can modify this line to make this query run?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make sure that lowdate is at least 1/1/1753.

If you try to supply a date prior to that, EF will convert it, and pass it into your query. In addition, you need to not use DateTime.MinValue in the query, but rather what would be your min:

DateOfBirth = ((s.Date_Of_Birth == null) || (s.Date_Of_Birth <= lowdate)) ?
    new DateTime(1753,1,1) : s.Date_Of_Birth.Value;

Remember, with EF, the query gets compiled and converted to SQL on the server, so the values must all be appropriate there, as well.

That being said, I'd personally prefer to store DateOfBirth as DateTime? (nullable type) instead of using a "magic value" (DateTime.MinValue) to hold database null or inappropriate values.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...