繁体   English   中英

SubSonic ActiveRecord为什么不更新我的数据?

[英]Why won't SubSonic ActiveRecord update my data?

我在玩SubSonic 3 ActiveRecord。 我有一个名为Users的表,并且在MySQL数据库中有以下字段:

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 (T项,IDataProvider提供程序)+113 D:\\ Users \\ Mike \\ Documents \\ Visual中的ServiceDesk.Data.User.Update(IDataProvider提供程序) Studio 2008 \\ Projects \\ dolphin \\ src \\ ServiceDesk.Web \\ Models_Generated \\ ActiveRecord.cs:1425 D:\\ Users \\ Mike \\ Documents \\ Visual Studio 2008 \\ Projects \\ dolphin \\ src中的ServiceDesk.Data.User.Save(IDataProvider提供程序) \\ ServiceDesk.Web \\ Models_Generated \\ ActiveRecord.cs:1461 D:\\ Users \\ Mike \\ Documents \\ Visual Studio 2008 \\ Projects \\ dolphin \\ src \\ ServiceDesk.Web \\ Models_Generated \\ ActiveRecord.cs中的ServiceDesk.Data.User.Save() :1452 D:\\ Users \\ Mike \\ Documents \\ Visual Studio 2008 \\ Pr中的ServiceDesk.Web.Controllers.SettingController.Edit(FormCollection结果) ojects \\ dolphin \\ src \\ ServiceDesk.Web \\ Controllers \\ SettingController.cs:48 lambda_method(ExecutionScope,ControllerBase,Object [])+140 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,Object []参数)+17系统.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary 2 parameters) +178 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2个参数)+24 System.Web.Mvc。<> c__DisplayClassa。 b__7()+52 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter过滤器,ActionExecutingContext preContext,Func 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.ControllerActionIn voker.InvokeAction(ControllerContext controllerContext,字符串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步骤,布尔值且已完成同步)+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”,但设置[“ FirstName”]不存在,但是设置[“ Firstname”]存在。 我在数据库中的列被命名为“ FirstName”,“ LastName”等,但亚音速ActiveRecord模板将对象字段命名为“ Firstname”和“ Lastname”

我的第一个猜测是,TimeZone是C#(System.TimeZone)中的保留字,尽管这实际上并不重要-这是我能想到的第一件事。 关键错误可能是SubSonic试图查找已重命名的名称为“ TimeZone”的属性-检查您的Structs.cs类/用户表是否是这种情况。

如果是这样,请提交错误。 如果没有,那么堆栈跟踪会有所帮助。

尝试更新行时,在3.0.0.4版本中也遇到了此异常。 我以前使用过SubSonic 1.0,但这是我第一次尝试使用v3-因此我可能忽略了过程中的某些内容。

我发现最快的解决方法是简单地删除行,然后直接将其插入。。这可能比仅更新要低效率-但在性能不是很关键的情况下可以接受。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM