问题
I have some .NET Core code that does some bulk loading of the DB with random sample data.
I'm getting 20 inserts/second on localhost, and looking to improve my performance. I'm doing the basic stuff like calling _dbContext.SaveChanges() only once, etc.
A number of posts like this indicate gains can be had by manipulating properties on the DbContext's Configuration, such as Configuration.AutoDetectChangesEnabled and Configuration.ValidateOnSaveEnabled.
My .NET Core MVC app's DbContext is a subclass of IdentityDbContext, which does not expose the Configuration.
Not sure what approach I should be using - can I / should I be messing with those configuration properties of a IdentityDbContext subclass?
Or, should I use a separate DbContext for this? (Some early research indicated the typical pattern is a single DbContext for a webapp).
回答1:
There is no need for creating separated DbContext class and you can turn change tracking off:
context.ChangeTracker.AutoDetectChangesEnabled = false;
or you can turn it off globaly:
public class MyContext : IdentityDbContext
{
public MyContext()
{
ChangeTracker.AutoDetectChangesEnabled = false;
}
}
来源:https://stackoverflow.com/questions/41412953/bulk-insert-optimization-in-net-ef-core