我在ASP Net CORE mvc项目中遇到了很多错误,但我没有找到解决它的方法:
A second operation started on this context before a previous operation
completed. Any instance members are not guaranteed to be thread safe
它附加到非异步函数上:
public Employee GetEmployeeByuserID(String userID)
{
return _applicationDBContext.Employee.Where(e => e.UserId.Equals(userID)).First();
}
我尝试使此功能异步,但它只是使错误附加到其他地方。
该函数位于具有_applicationDBContext依赖项注入的 DAL 中。 在启动的 AddTransient 中声明的 Dal .cs 我已经看到了一些等待的解决方案,但我找不到我应该把它放在哪里。
编辑
如果我在return _applicationDBContext.Employee.Where(e => e.UserId.Equals(userID)).First();
之前设置了一个断点来暂停代码 并继续,它工作得很好。
我最终通过为每个请求创建一个新的 DAL 来使其工作:
Employee emp = new EmployeeDal(new ApplicationDbContext()).GetEmployeeByuserID(appUser.Id);
如果您有更好的解决方案,请告诉我。