ihorko
ihorko

Reputation: 6945

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.

Upvotes: 4

Views: 32578

Answers (3)

Philip Johnson
Philip Johnson

Reputation: 1081

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.

Upvotes: 2

Sushil Kumar Naik
Sushil Kumar Naik

Reputation: 11

If you are using Convert.ToDateTime() in .Net code then please check the application hosted server's calendar setting, mainly the time zone and short date format

Upvotes: 0

Jakub Konecki
Jakub Konecki

Reputation: 46008

  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.

Upvotes: 5

Related Questions