1114_EF批量处理_终章

一、批量Update和Delete

  1. 向NuGet程序包中添加第三方类库:EntityFramework.Extended
  2. 具体方法:https://github.com/loresoft/EntityFramework.Extended/wiki/Batch-Update-and-Delete
    备注:目前此方法没有实现批量Insert。批量Insert的方法,如下所示。

二、批量Insert

批量Insert使用原生EF即可,举例如下:

                AppDatabase.DoAction(dbContext =>
                {
                    // 批量Insert操作,设置此属性为false即可解决性能底下的问题
                    dbContext.Configuration.AutoDetectChangesEnabled = false;

                    for (int i = 0; i < bedNoList.Count; i++)
                    {
                        EFModel.Machine machine = CreateMachine(bedNoList[i], true);
                        dbContext.Entry(machine).State = EntityState.Added;
                    }

                    CacheListener.DoBigJob(() =>
                    {
                        isSuccess = dbContext.SaveChanges() > 0;
                    });
                });

你可能感兴趣的:(1114_EF批量处理_终章)