Lets say I have a collection of 10,000 objects that I need to add to a database using Entity Framework (I recognize that EF isn't well-suited to this task, but let's run with it for now). For the purposes of this question, we'll make the following assumptions:
- There is only one table, with an
IDENTITYprimary key. - The table is empty.
- The objects are simple -- everything is a primitive data type (int, bool, string, etc.)
I could do this in Entity Framework in one of two ways:
// Option 1 foreach (var item in largeCollection) { _context.SomeTable.Add(item); } _context.SaveChanges(); // Option 2 _context.SomeTable.AddOrUpdate(largeCollection); _context.SaveChanges(); Is the performance of one method inherently better or worse than the other? Or do they both devolve into an equal number of single-line INSERT statements?
In other words, from a performance standpoint, is there any advantage to using Add() over AddOrUpdate() (or vice-versa) when inserting multiple items into a database?