首页 新闻 赞助 找找看

.net core 使用EF报错

0
悬赏园豆:20 [待解决问题]

错误提示: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.QueryingEnumerable1.Enumerator.MoveNext()

52Hertz程序人生的主页 52Hertz程序人生 | 初学一级 | 园豆:7
提问于:2020-11-26 17:21
< >
分享
所有回答(2)
0

不贴代码?

Jonny-Xhl | 园豆:691 (小虾三级) | 2020-11-26 19:33

是偶尔出现的报错
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("失败");
        }
    }
}
支持(0) 反对(0) 52Hertz程序人生 | 园豆:7 (初学一级) | 2020-11-26 20:06
1

dbcontext 被并发使用了. UserInfoService,IUnitOfWork 注册为scope. 不要注册为单例.

czd890 | 园豆:14292 (专家六级) | 2020-11-27 13:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册