“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

Prior to the completion of an operation, in this context to start a second operation. This is usually caused by different threads using the same dbcontext instance caused instance members but can not guarantee thread safety. This may also be due to the nested query on the client computing result, if this is the case, rewrite the query to avoid nested calls.

If DbContext using IoC and Dependency Injection then only need to

services.AddDbContext<Context>(options => options.UseSqlServer(connection));
这段代码修改为
services.AddDbContext<Context>(options => options.UseSqlServer(connection), ServiceLifetime.Transient);

ServiceLifetime.Transient:指定每次请求服务时都将创建该服务的新实例

Guess you like

Origin www.cnblogs.com/lyzi/p/11491216.html