Первая Tempdata имеет 352 000 записей. 2-я имеет 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 раз. И первый цикл while 2 сохраняется правильно, но последний цикл while застревает во временных таблицах и не сохраняет записи, оставленные для третьего цикла.
А вот и метод saveOrUpdateRange.
public void AddOrUpdateRange(IList<T> entities)
{
Context.Database.SetCommandTimeout(TimeSpan.FromMinutes(5));
Context.BulkInsertOrUpdate(entities);
Context.SaveChanges();
}
BulkInsertOrUpdate использует nuget EFCore.Bulkextensions.
Вот мои границы обслуживания.
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<>));