Reputation: 47
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();
}
Upvotes: 0
Views: 44
Reputation: 39
Please take a look at Unit of Work design pattern.
Upvotes: 1