迭代上下文类型的查询结果时发生异常。连接已关闭

And*_*osa 9 c# sql-server azure entity-framework-core

我在 LINQ 查询运行期间收到以下错误

迭代上下文类型的查询结果时发生异常。连接已关闭

奇怪的是,只有当应用程序在 Azure 上发布时(Db 也在 Azure 中)本地发布时才会发生这种情况,一切都像魅力一样

以下代码块会产生错误

List<StoreProductCatalogPrice> pricesToUpdate = await _storeProductCatalogPriceRepository.GetCurrentByProductCatalogId(productCatalogToUpdate);`
Run Code Online (Sandbox Code Playgroud)

注意productCatalogToUpdate是一个大的List<Guid>,大约有 7k Guid

存储库实施:

public async Task<List<StoreProductCatalogPrice>> GetCurrentByProductCatalogId(List<Guid> productCatalogsIds)
{          
    return await DbSet.Where(x => productCatalogsIds.Contains(x.StoreProductCatalogId)).ToListAsync();
}
Run Code Online (Sandbox Code Playgroud)

注 2:与上下文相关的所有内容均由本机 DI 处理,通过AddDbContext<T>()

知道为什么会发生这种情况吗?

And*_*osa 10

@GertArnold 绝对正确,谢谢伙计!

问题是 .Contains() 接收大量项目的速度太慢,并导致了这种奇怪的行为。为了解决这个问题,我遵循了@GertArnold 指出的答案,这将我带到了另一篇文章。

该策略是将大量项目分解为块并最终创建多个查询,这可能听起来很奇怪,许多查询比单个查询运行得更快,但在采用该解决方案之前我已经做了一些基准测试,代码被证明可以即使运行 14 个查询(在我的例子中)而不是单个查询,速度也会快 30% 左右。

这是最终的代码:

public async Task<List<StoreProductCatalogPrice>> GetCurrentByProductCatalogId(List<Guid> productCatalogsIds)
{
    var chunks = productCatalogsIds.ToChunks(500);
    return chunks.Select(chunk => DbSet.Where(c => chunk.Contains(c.StoreProductCatalogId))).SelectMany(x => x).ToList();
}
Run Code Online (Sandbox Code Playgroud)

此扩展方法IEnumerable<T>根据您通过参数传递的数量将单个拆分为较小的单个。此方法也由 @GertArnold 发布(再次感谢),可以在此处找到

public static IEnumerable<IEnumerable<T>> ToChunks<T>(this IEnumerable<T> enumerable, int chunkSize)
{
    int itemsReturned = 0;
    var list = enumerable.ToList(); // Prevent multiple execution of IEnumerable.
    int count = list.Count;
    while (itemsReturned < count)
    {
        int currentChunkSize = Math.Min(chunkSize, count - itemsReturned);
        yield return list.GetRange(itemsReturned, currentChunkSize);
        itemsReturned += currentChunkSize;
    }
}
Run Code Online (Sandbox Code Playgroud)