![](/img/trans.png)
[英]A second operation started on this context before a previous asynchronous operation completed
[英]EF error: A second operation started on this context before a previous asynchronous operation completed
我是 Blazor 的新手,并且在服务器端 Blazor 应用程序(.NET 5)中工作具有以下内容:
我有以下表格:
<EditForm Model="@MyObject" OnValidSubmit="Submit">
<DataAnnotationsValidator />
<ValidationSummary />
<InputText id="name" @bind-Value="MyObject.Name" />
<button type="submit">Submit</button>
</EditForm>
后面的代码:
public MyObject MyObject { get; set; } = new MyObject();
[Inject]
private IMyObjectService myObjectService { get; set; }
[Inject]
private NavigationManager navigationManager { get; set; }
void Submit()
{
var created = myObjectService.CreateMyObject(MyObject);
if (created != null)
{
navigationManager.NavigateTo("myobjects/manage");
} else
{
// do something
}
}
回购:
private readonly ApplicationDbContext _dbContext;
public MyObjectRepo(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<MyObject> CreateMyObject(MyObject MyObject)
{
_dbContext.Add(MyObject);
await _dbContext.SaveChangesAsync();
return MyObject;
}
public async Task<bool> DoesMyObjectExistByName(string name)
{
var exists = await _dbContext.MyObjects.AnyAsync(x => x.Name == name);
if (exists) return true;
return false;
}
然后我有一个MyObjectService
:
private readonly IMyObjectRepo _myObjectRepo ;
public MyObjectService(IMyObjectRepo myObjectRepo )
{
_myObjectRepo = myObjectRepo;
}
public async Task<MyObject> CreateMyObject(MyObject MyObject)
{
if (MyObject == null) throw new ArgumentNullException("MyObject cannot be null.");
// THIS LINE THROWS THE EF ERROR
var exists = await _myObjectRepo.DoesMyObjectExistByName(MyObject.Name);
if (!exists)
{
return await _myObjectRepo.CreateMyObject(MyObject);
} else
{
return null;
}
}
但是每次我调用服务的 createMyObject 方法时,EF 都会抛出一个错误:
错误:System.InvalidOperationException:在前一个操作完成之前,在此上下文上启动了第二个操作。 这通常是由不同的线程同时使用同一个 DbContext 实例引起的。
在 Startup.cs (.NET 5) 中,我配置了瞬态生命周期。
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Default")),
ServiceLifetime.Transient);
services.AddAutoMapper(typeof(Startup));
services.AddScoped<IMyObjectRepo, MyObjectRepo>();
services.AddScoped<IMyObjectService, MyObjectService>();
我想我正在使用所有正确的语法来实现异步/等待和线程安全,有什么想法吗?
我自己对Blazor
不熟悉,但我认为您的Submit
方法应该如下所示 -
async void Submit()
{
var created = await myObjectService.CreateMyObject(MyObject);
if (created != null)
{
navigationManager.NavigateTo("myobjects/manage");
} else
{
// do something
}
}
基本上,您缺少 async/await 关键字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.