错误提示:A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext. For more information on how to avoid threading issues with DbContext
百度翻译:在前一个操作完成之前,在这个上下文中启动第二个操作。这通常是由使用相同DbContext实例的不同线程造成的
构造函数注入方式是通过:UnitOfWork中依赖注入的DbContext
完整错误提示
ERROR Microsoft.EntityFrameworkCore.Query - An exception occurred while iterating over the results of a query for context type 'Context'.
System.InvalidOperationException: A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection()
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() System.InvalidOperationException: A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913. at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection() at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable
1.Enumerator.MoveNext()
不贴代码?
是偶尔出现的报错
db注入:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
#region 注册model
services.AddDbContext<saturn_inkjetContext>(options =>
options.UseMySql(Configuration.GetConnectionString("MySqlConnection")), ServiceLifetime.Transient);
services.AddUnitOfWork<saturn_inkjetContext>();//添加事务
#endregion
}
数据层调用Login方法偶尔回出现进入cath报上面错误提示,下次在调用恢复正常
public class UserInfoService : IUserInfoService
{
private readonly IUnitOfWork<dbContext> _unitOfWork;
private readonly dbContext _db;
private readonly ILogger<UserInfoService> _logger;
public UserInfoService(IUnitOfWork<dbContext> unitOfWork, ILogger<UserInfoService> logger)
{
_unitOfWork = unitOfWork;
_db = unitOfWork.DbContext;
_logger = logger;
}
public OperateResult<LoginUserModel> Login(string userName, string passWord)
{
try
{
var user = _db.Set<Users>().Where(l => l.LoginName == userName).FirstOrDefault();
LoginUserModel loginUser = new LoginUserModel();
loginUser.Id = user.Id;
loginUser.LoginName = user.LoginName;
loginUser.RealName = user.RealName;
loginUser.Gender = user.Gender;
loginUser.PersonPhone = user.PersonPhone;
loginUser.IdentityCard = user.IdentityCard;
loginUser.Email = user.Email;
loginUser.Company = user.Company;
string token = this._iJWTService.GetToken(loginUser);
loginUser.Token = token;
return OperateResult<LoginUserModel>.Ok("成功", loginUser);
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
return OperateResult<LoginUserModel>.Error("失败");
}
}
}
dbcontext 被并发使用了. UserInfoService,IUnitOfWork 注册为scope. 不要注册为单例.