开发者

How do you insert an IEnumerable<T> into entity model collection?

开发者 https://www.devze.com 2023-03-30 02:59 出处:网络
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:

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());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号