Reputation: 6945
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
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
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
Reputation: 46008
GETUTCDATE()
instead of GETDATE()
Upvotes: 5