Note that there are some explanatory texts on larger screens.

plurals
  1. POEF4 c# updating nested object in 1:n
    primarykey
    data
    text
    <p>I have an nested EF object hanging on its parent. It has 1:n relationship</p> <p>[parent]-[n..child]</p> <p>the nested object child is dynamic and will be updated through GUI.</p> <p>i have problem to updating it on database. </p> <p><strong>Error message:</strong> <em>An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.</em></p> <p>this is question is 2nd version. i made a correction on if block for deciding preExist</p> <p>Thanks in advance for your help</p> <p>sittingduck</p> <p>Update on Main</p> <pre><code>void MainUpdate { var context = new FamilyEntities(); parent = getParentFromGui(); parent.UpdateRelatedEntities(context); context.dispose(); } </code></pre> <p>the object parent was updated in Gui</p> <pre><code>parent getParentFromGui() { parent myParent = parentBindingSource.DataSource as parent; foreach(child c in childrenBindingSource) { myParent.children.Add(c); } return myParent } </code></pre> <p>Modified UpdateRelatedEntities</p> <pre><code>public static void UpdateRelatedEntities(this parent entity, FamilyEntities context) { if (entity == null) return; var res = context.parent.Where(x =&gt; x.uid_parent == entity.uid_parent); var rec = res.FirstOrDefault(); context.parent.Attach(rec); context.parent.ApplyCurrentValues(entity); foreach (var c in entity.children) { bool preExist = context.children.FirstOrDefault(x =&gt; x.child_uid == c.child_uid); if (preExist != null) { context.children.Attach(obj); context.children.ApplyCurrentValues(c); } else { // This Part throw ERROR context.children.AddObject(c); } } context.SaveChanges(); } </code></pre> <p>What i'm doing wrong? </p> <p>Tx alot!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload