【发布时间】:2021-04-02 03:26:49
【问题描述】:
在运行 LINQ 查询期间出现以下错误
迭代上下文类型的查询结果时发生异常。连接已关闭
奇怪的是,只有当应用程序在 Azure 上发布时才会发生这种情况(数据库也在 Azure 中)本地一切都像魅力一样工作
以下代码块产生错误
List<StoreProductCatalogPrice> pricesToUpdate = await _storeProductCatalogPriceRepository.GetCurrentByProductCatalogId(productCatalogToUpdate);`
注意:productCatalogToUpdate 是一个大的List<Guid>,大约有 7k 个 Guid
存储库实现:
public async Task<List<StoreProductCatalogPrice>> GetCurrentByProductCatalogId(List<Guid> productCatalogsIds)
{
return await DbSet.Where(x => productCatalogsIds.Contains(x.StoreProductCatalogId)).ToListAsync();
}
注意 2:与上下文相关的所有内容都由原生 DI 处理,通过 AddDbContext<T>()
知道为什么会这样吗?
【问题讨论】:
-
@mjwills 这是问题所在,我无法在本地重现,只有在应用发布后才会出现错误
-
再次阅读链接。简短的回答 - 您可能在 Web 请求的上下文之外使用 db 上下文。
-
Contains在处理大量数据时速度非常慢。应该是超时了。试试this。 -
你说得对,@GertArnold,这解决了我的问题,非常感谢!
标签: c# sql-server azure entity-framework-core