今天在热修生产环境的bug,测试环境没问题。然后根据报错信息,研究了一会,导致走了一些弯路。
我们在使用orm进行数据查询时,经常配合linq中的一些方法使用。尤其Where使用频率极高。
Error Info:
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.
常规导致的原因,目前项目都是处理好的。
注意:
只要返回的是IEnumerable对象的都有延迟执行特性
Select、Distinct、SelectMany
具体原因:就是使用where()等延迟加载的方法,默认是延迟执行–>查询的数据未加载到内存中,
所以使用其数据对应的变量,就没法进行任何相关操作.
解决方案:
转换为立即执行 ---->常用的方法:
FirstOrDefault、First、Single、ToDictionary、ToHashSet、ToList
参考的博客:
Linq Error A second operation started on this context before a previous operation completed