“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:指定每次请求服务时都将创建该服务的新实例

猜你喜欢

转载自www.cnblogs.com/lyzi/p/11491216.html
今日推荐