asp.net mvc: SQLDateTimeOverflow problem with Entity Framework
I'm trying to update an object, and getting:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
There are two fields in the object that are DateTime objects, and they're set with:
obj.created_date = DateTime.Now;
obj.modified_date = DateTime.Now;
Except that everything looks reasonable when I view the object:
>? obj.created_date
{1/13/2010 4:02:47 PM}
Date: {1/13/2010 12:00:00 AM}
Day: 13
DayOfWeek: Wednesday
DayOfYear: 13
Hour: 16
Kind: Unspecified
Millisecond: 817
Minute: 2
Month: 1
Second: 47
Ticks: 633989953678170000
TimeOfDay: {16:02:47.8170000}
Year: 2010
>? obj.modified_date
{1/19/2010 12:20:50 PM}
Date: {1/19/2010 12:00:00 AM}
Day: 19
DayOfWeek: Tuesday
DayOfYear: 19
Hour: 12
Kind: Local
Millisecond: 333
Minute: 20
Month: 1
Second: 50
Ticks: 633995004503331818
TimeOfDay: {12:20:50.3331818}
Year: 2010
Any idea what's going on? Even reading the fields from the database, then trying to save them back without changes, causes the error. Other classes have no problems, and I can't see any differences that would account for this error.
I've added
<globalizati开发者_StackOverflowon culture="en-US" />
to my web.config, with no changes.
These problems are on my local dev
OK, here's what was happening. marc_s had the right idea.
There's was binder handling the updates:
TryUpdateModel(editItem, new string[] { "LookupTable", "TextField1", "TextField2" });
I removed the "LookupTable" from the call to TryUpdateModel, and instead handle the value in the binder with:
var fkid = Convert.ToInt32(controllerContext.HttpContext.Request.Form["LookupTable.ID"]);
EntityKey ek = new EntityKey("entities.LookupTable", "ID", fkid);
obj.LookupTableReference.EntityKey = ek;
Apparently, the EF was trying to update the LookupTable value for some reason.
精彩评论