asp.net mvc - Loop Adding Records Throws Exception -


does know proper way of adding records using loops?

i have system handles inventory, need able mass create inventory creating 50-100 identical items different id's tedious, did create masscreate viewmodel take startid , endid , base inventory class , in controller loop through difference between 2 id's , create record

the viewmodel isn't issue , passes data fine:

public class masscreateinventoryviewmodel {     public inventory inventorybase { get; set; }     public int startid { get; set; }     public int endid { get; set; }     public ienumerable<selectlistitem> products { get; set; } } 

i read somewhere db.savechanges() should outside of loop should called once:

    (int inventoryid = viewmodel.startid; inventoryid <= viewmodel.endid; inventoryid++)                 {                     inventory newinventory = new inventory                         {                             inventoryid = inventoryid,                             productid = viewmodel.inventorybase.productid,                             dateedited = datetime.now,                             editedbyuserid = websecurity.currentuserid,                             customproperties = viewmodel.inventorybase.customproperties                         };                      database.inventories.add(newinventory);                      if (newinventory.customproperties != null && newinventory.customproperties.any())                     {                         foreach (customdatatype dt in newinventory.customproperties.select(x => x.datatype).tolist())                         {                             database.entry(dt).state = entitystate.unchanged;                         }                     }                  }                 database.savechanges();         } 

but when try looping, stores first record fine throws collection modified; enumeration operation may not execute. exception. when include database.savechanges() after add method, throws the property 'inventoryid' part of object's key information , cannot modified. error.

the inventoryid key in table has been set can input own id.

[key] [databasegenerated(databasegeneratedoption.none)] [display(name = "inventory id")] public new int inventoryid { get; set; } 

the custom property split 2 models, first being base class.

public class customproperty {     [key]     [databasegenerated(databasegeneratedoption.identity)]     public int custompropertyid { get; set; }      public int customdatatypeid { get; set; }     [foreignkey("customdatatypeid")]     public customdatatype datatype { get; set; }      public string propertyvalue { get; set; } } 

and second being model thats mapped database:

[table("custominventoryproperty")] public  class custominventoryproperty : customproperty {     public int inventoryid { get; set; }     [foreignkey("inventoryid")]     public virtual inventory inventory { get; set; } } 

replace loop this:

var dateedited = datetime.now; (int inventoryid = viewmodel.startid; inventoryid <= viewmodel.endid; inventoryid++) {     inventory newinventory = new inventory     {         inventoryid = inventoryid,          productid = viewmodel.inventorybase.productid,         dateedited = dateedited,         editedbyuserid = websecurity.currentuserid     };      if(viewmodel.inventorybase.customproperties != null)       {         newinventory.customproperties = new list<customproperties>();          foreach(var customproperty in viewmodel.inventorybase.customproperties)         {            newinventory.customproperties.add(customproperty);         }     }      database.inventories.add(newinventory);     database.savechanges(); } 

Comments