2011-12-15 7 views
6

nullのとき、私は次のようにしてEFの更新を実行しますが、このエラーを受信し続けるためにしようとしている設定することができます。のEntityKeyプロパティは唯一のプロパティの現在の値が

のEntityKeyプロパティのみ可能プロパティの現在の値がnullの場合に設定します。

 using (hydraEntities db = new hydraEntities()) 
     { 
      YouUser = db.youusers.Include("address").Include("entity").Include("youusercontacts.contact").Include("youuserlogins").Include("youusernotes.note").Include("youusernotes.youuser.entity").Where(yu => yu.YOUUserId.Equals(YOUUserId)).First(); 
     } 

      YouUser.entity.FirstName = txtFirstName.Text; 
      YouUser.entity.LastName = txtLastName.Text; 
      YouUser.address.AddressLine1 = txtAddressLine1.Text; 
      YouUser.address.AddressLine2 = txtAddressLine2.Text; 
      YouUser.address.City = txtCity.Text; 
      YouUser.address.State = ddlState.SelectedValue; 
      YouUser.address.Zipcode = txtZipcode.Text; 

      using (hydraEntities db = new hydraEntities()) 
      { 
       db.youusers.AddObject(YouUser); 
       db.ObjectStateManager.ChangeObjectState(YouUser, System.Data.EntityState.Modified); 
       db.SaveChanges(); 
      } 

が大幅に私はこの問題を解決し、上記の文を実行する方法上の任意の洞察力をお願い申し上げます。

答えて

10

このシナリオではAddObjectを使用しないでください。新しいエンティティを挿入するためですが、既存のエンティティを更新しています。私が追加された私のシナリオでは

using (hydraEntities db = new hydraEntities()) 
{ 
    db.youusers.Attach(YouUser); 
    db.ObjectStateManager.ChangeObjectState(YouUser, System.Data.EntityState.Modified); 
    db.SaveChanges(); 
} 
0

を別のスレッドによって一度に複数回オブジェクト:代わりにAttachを使用してください。このとき、モデルコンテナオブジェクトをロックして、一度に1つのオブジェクトしか処理されないようにしなければなりませんでした。

関連する問題