asp.net mvc - Adding two new elements to a List in Entity Framework -


in model, have following section class:

public partial class section {     public section()     {         steps = new list<sectionstep>();     }      public int id { get; set; }     public int sortid { get; set; }     public string name { get; set; }     public string title { get; set; }     public string html { get; set; }     public nullable<int> tutorial_id { get; set; }     [jsonignore]     public virtual tutorial tutorial { get; set; }     public virtual icollection<sectionstep> steps { get; set; }     [notmapped]     public bool _destroy { get; set; } } 

and following sectionstep class:

public class sectionstep {     public int id { get; set; }     public int sortid { get; set; }     public string name { get; set; }     public string title { get; set; }     public string html { get; set; }      [jsonignore]     public virtual section section { get; set; }      [notmapped]     public bool _destroy { get; set; } } 

i have asp.net application in users can edit these, including adding , removing sections , sectionsteps. data posted server via json, server reads correctly , adds fine. however, when add 2 sectionsteps same section, following error:

an object same key exists in objectstatemanager. objectstatemanager cannot track multiple objects same key.

the json posted server deserialized list<section> sections following code handles , adds appropriate objects entity model.

        foreach (section section in sections)             {                                     if (section._destroy)                 {                     // if id 0, never added                     if (section.id > 0)                         db.entry(section).state = entitystate.deleted;                 }                 else if (section.id == 0)                 {                     db.entry(section).state = entitystate.added;                 }                 else                 {                     db.entry(section).state = entitystate.modified;                 }                 foreach (sectionstep step in section.steps.tolist())                 {                                             if (step._destroy)                     {                         // if id 0, never added don't delete                         if (step.id > 0)                             db.entry(step).state = entitystate.deleted;                         else                             section.steps.remove(step);                     }                     else if (step.id == 0)                     {                         db.entry(step).state = entitystate.added;                         db.savechanges();                     }                     else                     {                         db.entry(step).state = entitystate.modified;                     }                 }             }             db.savechanges();             returnsections.addrange(db.sections.where(s => s.tutorial_id == tutorial_id).orderby(s => s.sortid).toarray()); 

it seems me problem when add 2 steps, both have same id reason. @ first start id = 0 because let database automatically assign that, handle sections same way, , adding 2 sections works fine.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -