为什么此AsParallel操作引发数据提供程序错误
本文关键字:程序 错误 数据 AsParallel 操作 为什么 | 更新日期: 2023-09-27 18:21:54
感谢您的关注。我有以下退货声明:
//Return the result set
return new FilterDto.FilterResult<Application>
{
Count = count,
Results = _results.ToList().AsParallel().Select(s => ConstructApplication(s))
};
它调用以下方法:
public Application ConstructApplication(Application application)
{
var result = new Application
{
Id = application.Id,
Title = application.Title,
Icon = application.Icon
. . .
};
return result;
}
CCD_ 1调用正在抛出";基础提供者未能打开";错误当CCD_ 2与实体框架一起使用时,这个问题已经得到了很好的证明。
问题
既然我在AsEnumerable()
之前调用ToList()
,那么我的集合现在是内存中的集合,难道不应该是线程安全的吗?
问题是我调用的构造函数中的一些属性是延迟加载的,所以我需要在调用AsParallel()
和使用例如.Include(i => i.Image)
的构造函数之前急切地加载它们。
有趣的是,即使不使用AsParallel()
,我的查询也能更快地加载相关实体。