In order to performance, exception capturing and etc., What are the differences between these two codes:
int count = 0;
foreach (var record in SomeDbEntityList)
{
count++;
dbContext.SomeDbEntity.Add(record);
if (count > 500)
{
dbContext.SaveChanges();
count = 0;
}
}
//LeftOver
if (count > 0)
dbContext.SaveChanges();
The direct code:
foreach (var record in SomeDbEntityList)
{
dbContext.SomeDbEntity.Add(record);
dbContext.SaveChanges();
}
Please take a look at Unit of Work design pattern.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.