There's a lot of information and answers on here and elsewhere about how to speed up bulk inserts with EF.
However, I'm only inserting about 10,000 records and the slow part of creating the entities.
First, I save the external data to a datatable, then loop through and for each row create a new entity, add the child entities in the loop (sourced from a couple of other datatables) and once the loop is finished call db.SaveChanges().
In my initial profiling, the db.SaveChanges() is slow, but nothing compared to the loop of creating all the objects in the first place - either as a separate List<entity> entities or directly to the context.
Is this usual? As of all the issues with bulk insert I can see, most seem to be related to the final commit to the database.
Edit to add some code Please excuse the psudo-code
DataTable ref1 = ConvertCSVtoDataTable(csv, firstRowsToDelete: 15); // Return's a Datatable from a CSV
foreach(string file in ListOfFilesToProcess)
{
DataTable tbl = loadExcelFiles(file);
foreach(DataRow dr in tbl.Rows)
{
Entity newEntity = new Entity();
Entity.property1 = dr["Property1"].ToString();
... // Keep mapping properties to elements in the datatable
Entity.Child.Add(new ChildEntity() { prop1 = ref1["ChildProp1"].ToString() });
// Add the newly created entity to the context
db.Entity.Add(newEntity);
}
}
// Save the context
db.SaveChanges();
So by the point of saving the context there's a few thousand newEntity objects and their child navigation objects.
Iterating over the datatable and creating all these objects is the part which is the slowest!
Also, db.Configuration.AutoDetectChangesEnabled = false; has already been set.
AddRange()I am inserting roughly 10,000 records in about 1.3 seconds. Something is going on here if it still takes 20 secs to complete.