A second operation started on this context before a previous operation completed--ABP

异常:

Mvc.ExceptionHandling.AbpExceptionFilter - 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 guaranteed to be thread safe. This could also be caused by a nested query being evaluated on the client, if this is the case rewrite the query avoiding nested invocations. 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, however instance members are not guaranteed to be thread safe.
This could also be caused by a nested query being evaluated on the client, if this is the case rewrite the query avoiding nested invocations.

异常代码:

原因:WhereIf引入的命名空间错误,using Abp.Collections.Extensions;改为 using Abp.Linq.Extensions;

根据查阅资料具体原因如下:

Abp.Collections.Extensions 返回的结果为IEnumerable,参数接收的是一个谓词表达式,也就是一个委托。委托一旦调用,就立即执行了,将执行结果保存在内存中。当结果量相当大时此方式容易崩溃

using Abp.Linq.Extensions 返回结果为IQueryable,接收的是一个表达式类型,表达式会存储拼接表达式树,直到在运行期最终执行。也就是此种方式是将表达式组装好好后再到数据库执行。

(个人理解)由此可见以上的代码,当使用using Abp.Collections.Extensions的queryL是一个委托,调用后就立即执行了,但还未完成的情况又调用了返回结果为IQueryable的query,此时就引发了上面的异常。所有解决方法是统一queryL与query的返回结果

此处最好的方式是都返回IQueryable即将引用空间变为using Abp.Linq.Extensions。

原文地址:https://www.cnblogs.com/sugarwxx/p/14705811.html