Which is better to use for adding IEnumerable object?

时间:2015-04-23 05:39:54

标签: asp.net repository-pattern dbcontext

I have found two ways to implement add operation on IEnumerable objects in repository pattern.

First is to use .AddRange() & pass the list in here. Or Second is to convert my list to Array & use .AddOrUpdate(ArrayObject).

Which is better to use if my intension is to remove foreach loop on IEnumerable items? Please help.

_context.DbSet<MyEntity>().AddOrUpdate(entities.ToArray());

Or

 _context.DbSet<MyEntity>().ToList().AddRange(entities);

1 个答案:

答案 0 :(得分:0)

可以使用新对象数组调用

AddOrUpdate ,因此您只需创建City []类型的数组并调用context.CitySet.AddOrUpdate(cc =&gt; cc.Id,cityArray );一旦。在生成的SQL中,应该有Where条件。

如果有表现,您想阅读以下内容

https://stackoverflow.com/a/21839455/194345

并查看以下内容:

context.BulkInsert(hugeAmountOfEntities);