[英]Dependency Injection of DbContext in .Net Core console app: A second operation started on this context before a previous operation completed
[英]EFCore 2.0 : A second operation started on this context before a previous operation completed
我明白錯誤在說什么,但無法實施解決方案。 我需要在線程中調用的方法中調用數據庫(只是為了選擇)。
我為此搜索了多個答案並嘗試添加ServiceLifetime.Transient
但得到了相同的結果。 看起來我的 DI 有問題。
我也曾嘗試將構造函數中的 Context 傳遞給PriceSheetAsyncRepository
但沒有成功。
public class PriceSheetRepository : IPriceSheetRepository
{
private readonly Pricing_PortalContext _context;
private readonly ILogger _logger;
public PriceSheetRepository(Pricing_PortalContext context, ILogger<PriceSheetRepository> logger)
{
_context = context;
_logger = logger;
}
public async Task<bool> ProcessPriceSheetImport(long ImportId)
{
var pricesheetAsyncRepo = new PriceSheetAsyncRepository(_logger);
foreach (var i in RequestList)
{
tasks.Add(Task.Run(async () => await pricesheetAsyncRepo.ValidatePublishAsync(i, _context)));
}
await Task.WhenAll(tasks);
}
}
public class PriceSheetAsyncRepository
{
private readonly ILogger _logger;
public PriceSheetAsyncRepository(ILogger logger)
{
_logger = logger;
}
public Task<bool> ValidatePublishAsync(PriceSheetImportModel i, MyDBContext _context)
{
return Task.Run(() =>
{
i.IsValid = true;
i.LinePart = i.LinePart?.ToUpper();
if (effDate != null && _context.MyTablePublish.Where(x => x.A == i.A).Any())
{
i.IsValid = false;
i.Message = i.Message + "Duplicate published record, ";
}
}
}
}
誰能指出我在這里做錯了什么?
簡單地說——實體框架上下文不是線程安全的,並且不允許多個操作並行運行。
如果您實際上有不同的上下文實例,則 Transient 有效。 但就您而言,您只是在多個並發任務上使用上下文。
要么使您的代碼成為單線程(for 循環內部等待,而不是添加到任務列表並使用 Task.WhenAll),要么為您要使用它的每個任務創建一個新的上下文實例。
后者的一個例子可能看起來像這樣(注意刪除 Task.Run - 它傷害了你,沒有幫助)
// in container builder
services.AddTransient<Pricing_PortalContext>();
services.AddTransient<Func<Pricing_PortalContext>>(sp => sp.GetRequiredService<Pricing_PortalContext>());
// inject a Func
public PriceSheetRepository(Func<Pricing_PortalContext> contextFactory, ILogger<PriceSheetRepository> logger)
// create a new func per task
tasks.Add(async () => await pricesheetAsyncRepo.ValidatePublishAsync(i, _contextFactory()));
雖然您的問題有正確答案,但看起來您正試圖用錘子殺死蒼蠅。 為你的問題召集一堆任務是矯枉過正的。
public async Task ProcessPriceSheetImport(long ImportId)
{
var ids = RequestList.Select(x => x.A);
var existent = new HasSet<int>(await context.MyTablePublish.Where(x => ids.Contains(x.A)).Select(x => x.A).ToList());
foreach (var i in RequestList)
{
if (existent.Contains(i.A))
{
i.IsValid = false;
i.Message = i.Message + "Duplicate published record, ";
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.