Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have Subscribers table in the DB which cantains DateCreate field that has default value GetDate(). When I try to add new record to the table via Entity Framework:

        Subscribers subs = new Subscribers();
        subs.Email = email;
        subs.DateCreate = DateTime.Now;
        DataBase.DBContext.AddToSubscribers(subs);
        DataBase.DBContext.SaveChanges();

it throws an exception: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

Who can help me? Thanks.

like image 634
ihorko Avatar asked Sep 18 '25 09:09

ihorko


2 Answers

  1. Make sure your machine clock is not in 15th century ;-)
  2. Recreate your model.
  3. It your table has default value you don't have to set it in code.
  4. Consider using GETUTCDATE() instead of GETDATE()
  5. Make sure there are no other DateTime fields without default values.
like image 181
Jakub Konecki Avatar answered Sep 20 '25 00:09

Jakub Konecki


This is a late answer, but I had the same problem just now. The reason for this problem is that you have another date in your entity, and the default (MinValue) for that will be 1/1/0001. In SQL/Server, the default (MinValue) is 1/1/1753 or something.

I found a great article that helped me out at vfstech.com, which suggests using datetime2 instead of datetime in the database, however this wasn't an option for me. However it also has code that does the translation both ways. I quickly created a class that inherited from my entity class and used that in my application and it solved my problem just fine without me having to infect my business logic with something that in my view, Microsoft should have implemented properly to begin with.

like image 31
Philip Johnson Avatar answered Sep 20 '25 02:09

Philip Johnson