2009-11-19 39 views
0

我正在玩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模板名爲「名字」和「姓氏」的對象字段

回答

1

我的第一個猜測是TimeZone是C#中的保留字System.TimeZone),雖然這真的不重要 - 這是我能想到的第一件事。關鍵錯誤可能是SubSonic正在嘗試查找名稱爲「TimeZone」的屬性,該屬性已被重命名 - 檢查Structs.cs類/ User表以查看是否屬於這種情況。

如果有,請提交錯誤。如果沒有 - 堆棧跟蹤會有所幫助。

+0

感謝羅布。我會試一試。我只是加載源並將其添加到我的項目中,但我會嘗試首先重命名TimeZone。 – 2009-11-19 21:56:32

+0

沒有運氣。我已經添加了堆棧跟蹤。我會稍後介紹代碼,看看我能否知道,除非你看到別的東西。 – 2009-11-19 22:06:31

+0

見上面,我發現問題,現在只需要糾正它 – 2009-11-20 00:10:24

0

當我嘗試更新一行時,我在3.0.0.4版本中也收到了這個異常。我以前使用SubSonic 1.0,但這是我第一次嘗試使用v3--所以我可能忽略了這個過程中的某些東西。

我發現的最快的解決方法是簡單地刪除該行,然後直接插入它。這可能比僅僅更新效率低 - 但在對性能不敏感的上下文中可能是可以接受的。

相關問題