Bulk Insert Optimization in .NET / EF Core

冷暖自知 提交于 2021-01-29 03:27:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!