c# - EF 6 SaveChanges with multiple references to same (changed) object -


i have class 2 references same class. when updating main class, may update referenced class. when have 2 references same (modified) object, invalidoperationexception:

attaching entity of type 'ns.entity' failed because entity of same type has same primary key value. can happen when using 'attach' method or setting state of entity 'unchanged' or 'modified' if entities in graph have conflicting key values. may because entities new , have not yet received database-generated key values. in case use 'add' method or 'added' entity state track graph , set state of non-new entities 'unchanged' or 'modified' appropriate.

simple example:

public class example {     public int oldestfriendid {get; set;}     public int bestfriendid {get; set;}     public virtual friend oldest {get; set; }     public virtual friend best {get; set; }   } 

if while updating example, want update middle name of oldest/best friend, works long aren't same. if same, above exception.

i can't figure out how work. i've tried setting references null, saving them independently of parent class, setting references in them null (ef automatically creating 2 list of examples in friend).

how can save object has changed when there multiple references it?

update: not yet working way want, have had progress after removing list of examples friend. also, update result of post. still investigating...

as sample code asked for...this post on web app, no change made

public actionresult saveedit(int id, [bind(include = "oldestfriendid, bestfrinedid, oldest, best")] example example) {     if (modelstate.isvalid)         {             using (((windowsidentity)controllercontext.httpcontext.user.identity).impersonate())             {                 using (var _db = new exampleentities())                 {                   //example.best= example.oldest; // line allow update work.                    //next line exception occurs                    _db.entry(example).state = entitystate.modified;                    _db.savechanges();                  }             }         } } 

the editorfor template:

@model testing.friend  <div class="col-md-10">     @html.hiddenfor(model => model.friendid)     @html.editorfor(model => model.firstname)     @html.editorfor(model => model.lastname) </div> 

the edit view example

@model testing.example  @using (html.beginform()) { @html.antiforgerytoken()  <div class="form-horizontal">     <h4>example</h4>     <hr />     @html.validationsummary(true, "", new { @class = "text-danger" })     @html.hiddenfor(model => model.exampleid)      <div class="form-group">         @html.labelfor(model => model.oldestfriendid, "oldestfriendid", htmlattributes: new { @class = "control-label col-md-2" })         @html.hiddenfor(model => model.oldestfriendid)         @html.editorfor(model => model.oldest)     </div>      <div class="form-group">         @html.labelfor(model => model.bestfriendid, "bestfriendid", htmlattributes: new { @class = "control-label col-md-2" })         @html.hiddenfor(model => model.bestfriendid)         @html.editorfor(model=> model.best)     </div>      <div class="form-group">         <div class="col-md-offset-2 col-md-10">             <input type="submit" value="save" class="btn btn-default" />         </div>     </div> </div> 

}

edit

the cause because when retrieve object back, deserializes 2 friends 2 different objects (even when same). same problem below, rather ef deserializing 2 objects, asp.net mvc doing it.

what have following:

  1. check if 2 friend id's same (as id pk). if not continue normal
  2. if have same id, check if 2 friend objects same.
  3. if same go step 5.
  4. combine changes together, want deal conflicts.
  5. set 1 of freinds same other friend reference, e.g. best = oldest
  6. savechanges()

original answer

my guess classic problem of include when retrieving data.

when

 context.examples.include(x => x.oldest).include(x => x.best).tolist() 

what happening ef create two objects of friend(oldest , best), if point same record. known problem include.

so when go save after update, ef sees them 2 separate entities same key (and data) , complains.

if case have couple of options:

  • retrieve list of friends current example , example without include
  • let ef use lazyloading , load friends when , need them.

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -