2016-09-19 2 views
0

でAレコードを更新するとき、私は録音が、プログラムが既にObjectStateManagerに存在する同じキーでこのエラーエラーEntityFramework

オブジェクトをキャッチ更新します。 ObjectStateManagerは同じキーで複数のオブジェクトを追跡することはできません。」

をこれが

public bool Update(User item, HttpPostedFileBase avatar) 
{ 
    var tran = ContextEntities.Database.BeginTransaction(IsolationLevel.ReadUncommitted); 
    try 
    { 
     var user = new UserDa().Get(ContextEntities, item.Id);//get current user 
     CheckConstraint(item, Enums.Status.Update); 
     //avatar checker 
     if (avatar != null) 
     { 
      if (avatar.ContentType != "image/jpeg") 
       throw new Exception("[Only Jpg Is Allowed"); 

      if (user.AvatarId == null) 
      { 
       item.AvatarId = new FileDa().Insert(ContextEntities, avatar); 
      } 
      else if (user.AvatarId != null) 
      { 
       item.AvatarId = new FileDa().Update(ContextEntities, (Guid)user.AvatarId, avatar); 
      } 
     } 
     //password checker 
     item.Password = string.IsNullOrWhiteSpace(item.Password) ? user.Password : Utility.Hash.Md5(item.Password); 
     ContextEntities.Entry(item).State = EntityState.Modified; 
     if (!new UserDa().Update(ContextEntities, item)) 
      throw new Exception(); 
     tran.Commit(); 
     return true; 
    } 
    catch (Exception ex) 
    { 
     tran.Rollback(); 
     throw new Exception(ex.Message); 
    } 
} 

私のコードであり、これはエラーを示し、なぜ

UserDaクラスで
public bool Update(PortalEntities contextEntities, User item) 
{ 
    var res = contextEntities.SaveChanges() > 0; 
    return res; 
} 

私の更新方法であり、どのように修正できますか?

+0

おそらくインスタンスので 'item'は既に追跡されている今、あなたは、ライン' VARユーザー=新しいUserDa()内の重複インスタンスを取得または追加している(ContextEntities、item.Id)を取得し; //現在の取得しますユーザ。しかし、あなたはこのメソッドのコードを提供していないので、ちょうど投機です。 – Igor

答えて

0

Igorが正しいと思います。この問題を解決するには、ユーザーが既にデータベースを更新し、アイテムのプロパティではなくユーザーのプロパティを更新します。コード行「ContextEntities.Entry(item).State = EntityState.Modified;」を削除します。変更を保存します。

public bool Update(User item, HttpPostedFileBase avatar) 
{ 
    var tran = ContextEntities.Database.BeginTransaction(IsolationLevel.ReadUncommitted); 
    try 
    { 
     var user = new UserDa().Get(ContextEntities, item.Id);//get current user 
     CheckConstraint(item, Enums.Status.Update); 

     if(user == null) 
     { 
      //throw and error or do something else 
     }} 

     //avatar checker 
     if (avatar != null) 
     { 
      if (avatar.ContentType != "image/jpeg") 
       throw new Exception("[Only Jpg Is Allowed"); 

      if (user.AvatarId == null) 
      { 
       user.AvatarId = new FileDa().Insert(ContextEntities, avatar); 
      } 
      else if (user.AvatarId != null) 
      { 
       user.AvatarId = new FileDa().Update(ContextEntities, (Guid)user.AvatarId, avatar); 
      } 
     } 
     //password checker 
     user.Password = string.IsNullOrWhiteSpace(item.Password) ? user.Password : Utility.Hash.Md5(user.Password); 
     //ContextEntities.Entry(item).State = EntityState.Modified; 
     if (!new UserDa().Update(ContextEntities, user)) 
      throw new Exception(); 
     tran.Commit(); 
     return true; 
    } 
    catch (Exception ex) 
    { 
     tran.Rollback(); 
     throw new Exception(ex.Message); 
    } 
} 
+0

また、アイテム上で更新された可能性のある他のユーザープロパティも必ず更新してください。 AutoMapperはこの種のものに適しています。 – Sooey