本文介绍了.NET/EF Core中的批量插入优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些.NET Core代码,可以使用随机样本数据对数据库进行一些批量加载.

I have some .NET Core code that does some bulk loading of the DB with random sample data.

我在本地主机上每秒获得20次插入,并希望提高性能.我正在做一些基本的事情,例如只调用一次 _dbContext.SaveChanges(),等等.

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.

许多帖子,例如表示可以通过操纵DbContext的属性来获得收益配置,例如 Configuration.AutoDetectChangesEnabled Configuration.ValidateOnSaveEnabled .

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.

我的.NET Core MVC应用程序的DbContext是 IdentityDbContext 的子类,它不公开配置.

My .NET Core MVC app's DbContext is a subclass of IdentityDbContext, which does not expose the Configuration.

不确定我应该使用哪种方法-我可以/应该弄混IdentityDbContext子类的那些配置属性吗?

Not sure what approach I should be using - can I / should I be messing with those configuration properties of a IdentityDbContext subclass?

或者,我应该为此使用单独的DbContext吗?(一些早期研究表明,典型模式是Web应用程序的单个DbContext.)

Or, should I use a separate DbContext for this? (Some early research indicated the typical pattern is a single DbContext for a webapp).

推荐答案

无需创建单独的 DbContext 类,您可以关闭更改跟踪:

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;
    }
}

这篇关于.NET/EF Core中的批量插入优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 10:20