0

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);
Neha
  • 2,933
  • 3
  • 19
  • 23

1 Answers1

0

AddOrUpdate can be called with an array of new objects, so you can just create an array of type City[] and call context.CitySet.AddOrUpdate(cc => cc.Id, cityArray); once. In generated SQL, there should be Where condition.

In case of performance, you would like to read following

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

and look following:

context.BulkInsert(hugeAmountOfEntities);
Community
  • 1
  • 1
Brij
  • 6,086
  • 9
  • 41
  • 69
  • Thanks @Web World, I have gone through this for the ways when am trying to implement this. And its really fair to use BulkInsert. But I want to know which is preferable when I compare in between AddOrUpdate with array or AddRange(). – Neha Apr 23 '15 at 08:29
  • @Neha You can check with generated SQL of both (using SQL Profiler or VS debug windows) and get idea. As I said, AddOrUpdate should have Where in SQL command so AddRange should be better but not sure. – Brij Apr 23 '15 at 09:12
  • I find both sqlcommand is having Where. Will you please clarify more on what you mean by where in sql command. I mean the difference which I have to look into. – Neha Apr 23 '15 at 09:45
  • Compare both generated SQLs if both are same then both AddOrUpdate and AddRange would be same in your case. – Brij Apr 23 '15 at 09:52
  • Ok, I have same sql. Thanks :) – Neha Apr 23 '15 at 11:20