How do you insert an IEnumerable<T> into entity model collection?
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));
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".
Maybe something like:
var missingitems = IEnumerable<MissingItem>();
//Add lots of Missing Items
modelContext.MissingItems.AddList(missingitems.ToList());
精彩评论