Entity Framework and conversion of a datetime2

Have you ever got a conversion error when saving an entity with Entity Framework? Even with Entity Framework 5.0, you may receive the following error message that the value is out of rage.

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

This is the case if you have a date time set to 0001-01-01 because it’s out of range for SQL Server (even with Sql Server 2008 R2). In fact, SQL Server date can have a date only after the year 1753.


The solution to this problem is to set a property date or to make nullable this field in the database and into your entity. This way, you could set to null if the date is not set. Otherwise, you can use instead of 0001-01-01 another date has a “not set date” which is over 1753-01-01.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

4 thoughts on “Entity Framework and conversion of a datetime2

  1. what mostly causes this error is because your date field in the database is set to not null, to resolve it, set it to a nullable datetime and the error will not occur again

    • Hello Aduwu, you are right. I actually propose that solution in the article to set the type to allow NULL. Thanks for highlighting the solution, it brings back some old memory of 6 years ago 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.