“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, however instance members are not g
在上一個操作完成之前,在此上下文上啟動了第二個操作。這通常是由使用同一dbcontext實例的不同線程造成的,但是實例成員不能保證線程安全。這也可能是由於在客戶端上計算的嵌套查詢造成的,如果是這種情況,請重寫查詢以避免嵌套調用。
如果DbContext使用的是IoC和依賴注入那么只需要將
services.AddDbContext<Context>(options => options.UseSqlServer(connection));
這段代碼修改為
services.AddDbContext<Context>(options => options.UseSqlServer(connection), ServiceLifetime.Transient);
ServiceLifetime.Transient:指定每次請求服務時都將創建該服務的新實例