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

asp.net database entity-framework

Question

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.

1
3
2/23/2012 3:01:52 PM

Accepted Answer

  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.
4
2/23/2012 3:05:06 PM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow