批量插入记录时,临时表在操作结束后不会删除,并且在第三个临时表上插入失败

时间:2018-10-31 06:44:21

标签: entity-framework asp.net-core bulkinsert

enter image description here

第一个Tempdata有352.000条记录,第二个有146.000条记录,第三个有232.000条记录。

这种情况。这里是执行此操作的代码。

            while (true)
            {
                var result = carDatabaseClient.GetInvoiceLines(authorizedService, authorizedService.LastInvoiceLineUpdate, lastId);

                if (result != null)
                {
                    if (result.Rows.Count != 0)
                    {
                        var dataRows = Functions.Convert<InvoiceLine>(result).Select(s =>
                        {
                            s.Id = authorizedService.Id + "-" + s.DcId;
                            s.AuthorizedServiceId = authorizedService.Id;
                            if (!string.IsNullOrWhiteSpace(s.InvoiceId)) s.InvoiceId = authorizedService.Id.ToString() + "-" + s.InvoiceId;
                            return s;
                        }).ToList();

                        invoiceLineService.SaveOrUpdateRange(dataRows);
                        documentsTotal += dataRows.Count;
                        lastId = dataRows.LastOrDefault()?.DcId;
                        Thread.Sleep(2000);
                    }
                    else
                        break;
                }
                else
                    break;
            }

每个查询保存500.000条记录。 并且重复大约3次。而且前2个while循环可以正确保存,但最后一个while循环会卡在临时表中,并且不会保存任何留给第3个循环的记录。

这是saveOrUpdateRange方法。

    public void AddOrUpdateRange(IList<T> entities)
    {
        Context.Database.SetCommandTimeout(TimeSpan.FromMinutes(5));
        Context.BulkInsertOrUpdate(entities);
        Context.SaveChanges();
    }

BulkInsertOrUpdate使用EFCore.Bulkextensions nuget。

这里是我的服务范围。

        services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
        //services.AddDbContextPool<ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking));
        services.AddDbContext<ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        //services.AddDbContext<ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));

0 个答案:

没有答案
相关问题