Created
March 18, 2022 12:14
-
-
Save Guifgr/be67f0d0aa952f077eb1ecda21a8741e to your computer and use it in GitHub Desktop.
MultiThread save changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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