我正在玩SubSonic 3 ActiveRecord。我有一个名为用户的表,我在MySQL数据库中有以下字段:为什么SubSonic ActiveRecord不会更新我的数据?
CREATE TABLE `users` (
`ID` int(10) NOT NULL auto_increment,
`Username` varchar(50) NOT NULL,
`FirstName` varchar(50) default NULL,
`LastName` varchar(50) default NULL,
`EmailAddress` varchar(100) default NULL,
`OfficePhone` varchar(25) default NULL,
`MobilePhone` varchar(25) default NULL,
`TimeZone` varchar(25) default NULL,
`LastLogin` datetime default NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1
我可以从表中读取没有问题。我可以添加没有问题的新记录,但由于某些原因,我无法更新现有记录。我浏览了代码,editUser对象中包含正确的数据。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(FormCollection result)
{
var editUser = ServiceDesk.Data.User.SingleOrDefault(x => x.ID == Convert.ToInt32(result["ID"]));
UpdateModel(editUser);
editUser.Save();
return RedirectToAction("Index", "Home");
}
以下是错误我得到:
Server Error in '/' Application.
The given key was not present in the dictionary.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
Source Error:
Line 1423:
Line 1424: if(this._dirtyColumns.Count>0)
Line 1425: _repo.Update(this,provider);
Line 1426: OnSaved();
Line 1427: }
Source File: D:\Users\Mike\Documents\Visual Studio 2008\Projects\dolphin\src\ServiceDesk.Web\Models\_Generated\ActiveRecord.cs Line: 1425
任何人都可以提供一些帮助?
这里是堆栈跟踪: [KeyNotFoundException:给定的键不存在在词典] System.ThrowHelper.ThrowKeyNotFoundException()28 System.Collections.Generic.Dictionary 2.get_Item(TKey key) +7456284 SubSonic.Extensions.Database.ToUpdateQuery(T item, IDataProvider provider) +642 SubSonic.Repository.SubSonicRepository
1.Update(T项,IDataProvider provider)+113 D:\ Users \ Mike \ Documents \ Visual Studio 2008 \ Projects \ dolphin \ src \ ServiceDesk.Web \ Models_Generated \ ActiveRecord.cs中的ServiceDesk.Data.User.Update(IDataProvider提供程序):1425 ServiceDesk.Data.User.Save(IDataProvider提供程序)位于D:\ Users \ Mike \ Documents \ Visual Studio 2008 \ Projects \ dolphin \ src \ ServiceDesk.Web \ Models_Generated \ ActiveRecord.cs中:1461 ServiceDesk.Data.User.Save ()在D:\ Users \ Mike \ Documents \ Visual Studio 2008 \ Project中s \ dolphin \ src \ ServiceDesk.Web \ Models_Generated \ ActiveRecord.cs:1452 ServiceDesk.Web.Controllers.SettingController.Edit(FormCollection result)in D:\ Users \ Mike \ Documents \ Visual Studio 2008 \ Projects \ dolphin \ src \ ServiceDesk.Web \ Controllers \ SettingController.cs:48 lambda_method(ExecutionScope,ControllerBase,Object [])+140 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object [] parameters)+17 System.Web .Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary 2 parameters) +178 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters)+24 System.Web.Mvc。 <> c__DisplayClassa.b__7()52 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter滤波器,ActionExecutingContext preContext,函数功能1 continuation) +254 System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList
1个滤波器,ActionDescriptor actionDescriptor,IDictionary`2参数)192 System.Web.Mvc.ControllerActionInvoker。 InvokeAction(ControllerContext controllerContext,String actionName)+399 System.Web.Mvc.Controller.ExecuteCore()+126 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)+27 System.Web.Mvc.ControllerBase.System .Web.Mvc.IController.Execute(RequestContext requestContext)+7 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)+151 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext)+57 System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext的HttpContext的)7 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()181 System.Web.HttpApplication.ExecuteStep (IExecutionStep步骤,布尔& completedSynchronously)75
貌似问题是在这样的代码:
public static ISqlQuery ToUpdateQuery<T>(this T item, IDataProvider provider) where T : class, new()
{
Type type = typeof(T);
var settings = item.ToDictionary();
ITable tbl = provider.FindOrCreateTable<T>();
Update<T> query = new Update<T>(tbl.Provider);
if(item is IActiveRecord)
{
var ar = item as IActiveRecord;
foreach(var dirty in ar.GetDirtyColumns())
{
if(!dirty.IsPrimaryKey && !dirty.IsReadOnly)
query.Set(dirty.Name).EqualTo(settings[dirty.Name]);
}
}
dirty.Name = “姓”,但设定[ “姓”]不存在,但是设置[“名字”]确实存在。我的数据库中的列被命名为“FirstName”,“LastName”等,但亚音速ActiveRecord模板名为“名字”和“姓氏”的对象字段
感谢罗布。我会试一试。我只是加载源并将其添加到我的项目中,但我会尝试首先重命名TimeZone。 – 2009-11-19 21:56:32
没有运气。我已经添加了堆栈跟踪。我会稍后介绍代码,看看我能否知道,除非你看到别的东西。 – 2009-11-19 22:06:31
见上面,我发现问题,现在只需要纠正它 – 2009-11-20 00:10:24