4
I’m entering an average of 50,000 records into the database via the Entity Framework. And I got a question regarding the following code placements: in the first (1) placement, I use a foreach
creating the objects with your data and adding them (contexto.addToTabela
), using the SaveChanges()
outside the foreach
, at the end of the whole process, being used only once. The second (2) placement would be using the SaveChanges()
within the foreach, that is, to every added object there is a commit
. What is the difference between the two situations?
code example:
situation 1:
using (entities contexto = new entities())
{
foreach (var item in lista)
{
...
contexto.AddToTabela(item);
}
contexto.SaveChanges();
}
situation 2:
using (entities contexto = new entities())
{
foreach (var item in lista)
{
...
contexto.AddToTabela(item);
contexto.SaveChanges();
}
}
Have you considered a mass insertion?
– Jedaias Rodrigues