2011-12-13 49 views
8

在我的應用程序有以下代碼...實體框架錯誤:用空的EntityKey值的對象不能附加到對象上下文

public Boolean SaveUserInformation(UserInfoDTO UserInformation) 
    { 
     return dataManager.SaveUserInfo(new UserInfo() 
     { 
      UserInfoID = UserInformation.UserInfoID.HasValue ? UserInformation.UserInfoID.Value : 0, 
      UserID = UserInformation.UserID, 
      ProxyUsername = UserInformation.ProxyUsername, 
      Email = UserInformation.Email, 
      Status = UserInformation.Status 
     }); 
    } 

此代碼調用利用DATAMANAGER對象的方法實體框架...

public Boolean SaveUserInfo(UserInfo userInfo) 
    { 
     try 
     { 
      //Validate data prior to database update 
      if (userInfo.UserID == null) { throw new Exception("UserInfoDomainModel object passed to PriorityOne.Data.DataManager.SaveUserInfo with UserID property set to NULL."); } 
      if (userInfo.ProxyUsername == null) { throw new Exception("UserInfoDomainModel object passed to PriorityOne.Data.DataManager.SaveUserInfo with ProxyUsername property set to NULL."); } 
      if (userInfo.Email == null) { throw new Exception("UserInfoDomainModel object passed to PriorityOne.Data.DataManager.SaveUserInfo with Email property set to NULL."); } 

      if (userInfo.UserInfoID == 0) 
      { 
       //Perform Insert 
       using (PriorityOneEntities entities = new PriorityOneEntities()) 
       { 
        entities.UserInfoes.AddObject(userInfo); 
        entities.SaveChanges(); 
       } 
      } 
      else 
      { 
       //Perform Update 
       using (PriorityOneEntities entities = new PriorityOneEntities()) 
       { 
        entities.Attach(userInfo); 
        entities.SaveChanges(); 
       } 
      } 

      return true; 
     } 
     catch (Exception ex) 
     { 
      //TODO: Log Error 
      return false; 
     } 

    } 

該代碼插入工作得很好。但是,當我嘗試執行更新我收到一個錯誤說:「有一個空的EntityKey值的對象不能附加到對象上下文」

它發生在這條線的代碼:entities.Attach(USERINFO);

我試圖做到的是要避免一個往返到數據庫只是選擇我將在稍後進行修改和更新記錄,從而使往返兩次到數據庫。

任何想法什麼錯誤,或者我如何能更好地做到這一點?

謝謝。

回答

20

好像你正在使用EF 4.1+
你必須告訴你希望你的實體進行更新EF(修改狀態):

//Perform Update 
using (PriorityOneEntities entities = new PriorityOneEntities()) 
{ 
    entities.Entry(userInfo).State = EntityState.Modified; 
    entities.SaveChanges(); 
} 

附:您不必明確呼叫Attach。這是在引擎蓋下完成的。

更新
根據您的意見,您使用的是EF 4.0。這裏是你必須做什麼才能附上對象修改EF 4.0:

ctx.AddObject("userInfoes", userInfo); 
ctx.ObjectStateManager.ChangeObjectState(userInfo, EntityState.Modified); 
ctx.SaveChanges(); 

不能使用Attach方法。從http://msdn.microsoft.com/en-us/library/bb896271.aspx

If more than one entity of a particular type has the same key value, the Entity Framework will throw an exception. To avoid getting the exception, use the AddObject method to attach the detached objects and then change the state appropriately.

3

MSDN

The object that is passed to the Attach method must have a valid EntityKey value. If the object does not have a valid EntityKey value, use the AttachTo method to specify the name of the entity set.

希望這會幫助你。

+0

我利用的AttachTo方法是這樣的:entities.AttachTo(entities.UserInfoes.EntitySet.Name,USERINFO);而這需要處理錯誤。我不再有例外,但我沒有看到數據庫中的更改。 – jdavis 2011-12-14 15:26:08

+1

嘗試此[文章](http://geekswithblogs.net/michelotti/archive/2009/11/27/attaching-modified-entities-in-ef-4.aspx)。 – Shymep 2011-12-14 17:17:40

相關問題