I am using EF6 in a project. Because of the speed limitations for adding many (i.e. 10,000+) entities via EF6 I have resorted to doing the database additions outside of EF6. The code that does the database 'Add' returns a list of entity objects that represent the new database rows.
I then want to attach these new entities to my repository so that the Database and the repository are in sync. To do this I am using the following method:
public void AttachRangeNoTracking<TEntity>(IEnumerable<TEntity> entities) where TEntity : class
{
try
{
_context.Configuration.AutoDetectChangesEnabled = false;
foreach (var entity in entities)
{
_context.Set<TEntity>().Attach(entity);
}
}
finally
{
_context.Configuration.AutoDetectChangesEnabled = true;
}
}
The problem is that this code is quite time consuming when it is passed many entity objects. In tests 5000 entities could take 18 secs which is way to long.
Are there any other performance improvements or alternative approaches that might be quicker for attaching large numbers of entities to the DbContext
?