Skip to content

Instantly share code, notes, and snippets.

@offpepe
Forked from Guifgr/repository.cs
Created March 31, 2023 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save offpepe/58e46de0c9240cd1070315617263d11e to your computer and use it in GitHub Desktop.
Save offpepe/58e46de0c9240cd1070315617263d11e to your computer and use it in GitHub Desktop.
MultiThread save changes
public void SaveChangesParallel(List<objeto> objetos)
{
var saveList = new List<List<objeto>>();
var count = 0;
while (count < objetos.Count)
{
var partialSaves = objetos.Skip(count).Take(250).ToList();
count += partialSaves.Count;
saveList.Add(partialSaves);
}
if (saveList.Count < 1)
{
throw new BadRequestException("faltam objetos");
}
Parallel.ForEach(saveList, file =>
{
using var context = new Context();
context.objetos.UpdateRange(file);
context.SaveChanges();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment