0

I have an IEnumerable and I want to insert all the items into an Entity Model collection. e.g. Looking for a way to do the following:

var missingitems = IEnumerable<MissingItem>();
//Add lots of Missing Items
modelContext.MissingItems.AddList(missingitems);

Instead of having to do:

missingitems.ToList().ForEach(mi=>modelContext.MissingItems.Add(mi));
BlueChippy
  • 5,935
  • 16
  • 81
  • 131
  • Check [Add range to ICollection](http://stackoverflow.com/questions/1667614/t-in-class-addrange-icollection/1667634#1667634) answer – Eranga Aug 21 '11 at 09:18

2 Answers2

1

There is no AddList or AddRange. You can create your own extension method which will offer you your expected syntax but internally your new method will still call AddObject (or Add in DbContext) in loop because AddObject (Add) is not just adding to "collection".

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
0

Maybe something like:

var missingitems = IEnumerable<MissingItem>();
//Add lots of Missing Items  
modelContext.MissingItems.AddList(missingitems.ToList());
Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137