I have written a method to bulk update/delete/insert rows into a database table using Entity Framework. I have pasted the code below. The DBTable has 23 columns and the DBTableRow is a class which has properties that map to each column of the DBTable. The input parameters to the method are IEnumerables, on which some comparison is done using a custom equality comparer to arrive at the list of rows that need to be added, deleted or modified. Typically, the enumerable can get to a size of 50000-60000.
The problem I am facing is with the method's slowness. For a net of 200 rows (across all operations - add, delete and update), it takes 30 minutes. For a net of 2000 rows, it has taken close to 6 hours and hasn't finished yet.
Could the experts point out the performance bottlenecks in the code? Thanks a lot in advance...
private void InsertIntoDB(DbContext dbContext, IEnumerable<DBTableRow> fromLatestB, IEnumerable<DBTableRow> olderB,
IEnumerable<DBTableRow> toBeAddedB, IEnumerable<DBTableRow> toBeDeletedB,
IEnumerable<DBTableRow> toBeModifiedB, IQueryable<int> listMultiple)
{
dbContext.Configuration.AutoDetectChangesEnabled = false;
dbContext.Configuration.ValidateOnSaveEnabled = false;
int TypeId = 30;
if (toBeAddedB != null && toBeAddedB.Any())
toBeAddedB.ToList().ForEach(s => dbContext.DBTable.Add(s));
if (toBeDeletedB != null && toBeDeletedB.Any())
{
toBeDeletedB.ToList().ForEach(s =>
{
if (s.Type == TypeId)
{
var rlRows = dbContext.DBTable.Where(x => x.Type == TypeId && x.Url.Equals(s.Url, StringComparison.OrdinalIgnoreCase));
if (rlRows != null && rlRows.Any())
{
rlRows.ToList().ForEach(y =>
{
if (dbContext.Entry(y).State == EntityState.Detached)
dbContext.DBTable.Attach(y);
dbContext.DBTable.Remove(y);
});
}
}
else
{
dbContext.DBTable.Attach(s);
dbContext.DBTable.Remove(s);
}
});
}
if (toBeModifiedB != null && toBeModifiedB.Any())
{
var eqComp = new CustomEqualityComparer(listMultiple);
var toBeModifiedNew = fromLatestB.Intersect(olderB, new CustomEqualityComparer(true, listMultiple));
toBeModifiedB.ToList().ForEach(x =>
{
var rowNew = ReturnRowFromModifiedNewList();
if (rowNew != null)
{
x.Type = rowNew.Type;
x.Url = rowNew.Url;
x.Data = rowNew.Data;
x.LastModified = DateTime.UtcNow;
dbContext.Entry(x).State = EntityState.Modified;
}
});
}
dbContext.SaveChanges();
dbContext.Configuration.AutoDetectChangesEnabled = true;
dbContext.Configuration.ValidateOnSaveEnabled = true;
}