I'm testing out a bulk import using EF Core and trying to save asynchronously.
I'm trying to add 100 entities at a time and then asynchronously save and repeat until they are all saved. I occasionally get a PK error because it tries to add two entities with the same id to the database. None of the entities being added have an id set, the ids are sequences that are auto generated.
The code:
public async Task<bool> BulkAddAsync(IEnumerable<VehicleCatalogModel> models)
{
_dbContext.ChangeTracker.AutoDetectChangesEnabled = false;
try
{
var entities = models.Select(ToEntity);
_dbContext.Set<VehicleCatalog>().AddRange(entities);
await _dbContext.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError(0, ex, "An Error occurred during the import");
return false;
}
return true;
}
I am calling the method in an xunit test that generates a list of test data and calls the import
var result = manager.BulkAddAsync(modelsToAdd.AsEnumerable());
var counter = 0;
while (!result.IsCompleted && counter < 10)
{
Thread.Sleep(6000);
counter++;
}
Assert.True(result.IsCompleted && result.Result);
It should hold up the execution after 100 entities have been added until they are saved, and then add more but I am still occasionally getting this error. Is there something else I need to add to get this to work correctly? Or a better method of bulk insert?