0x80131904 dotnet data_error ai_generated true

System.InvalidOperationException: 执行查询时发生错误。查询已被取消。有关详细信息,请参见InnerException。 -> Microsoft.Data.SqlClient.SqlException (0x80131904): 执行超时已过期。在操作完成之前超时时间已到,或者服务器未响应。

System.InvalidOperationException: An error occurred while executing the query. The query has been canceled. See InnerException for details. -> Microsoft.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

ID: dotnet/ef-core-query-split-query-timeout

其他格式: JSON · Markdown 中文 · English
85%修复率
85%置信度
1证据数
2023-08-05首次发现

版本兼容性

版本状态引入弃用备注
EF Core 5.0 active
EF Core 6.0 active
EF Core 7.0 active
EF Core 8.0 active
SQL Server 2016+ active

根因分析

Entity Framework Core的分割查询模式(通过AsSplitQuery()启用)生成按顺序执行的多个SQL查询,总时间超过默认的30秒命令超时,尤其是在大型数据集或多个Include的情况下。

English

Entity Framework Core's split query mode (enabled via AsSplitQuery()) generates multiple SQL queries that execute sequentially, and the total time exceeds the default 30-second command timeout, especially with large datasets or multiple includes.

generic

官方文档

https://learn.microsoft.com/en-us/ef/core/querying/single-split-queries

解决方案

  1. Increase the command timeout specifically for the problematic query using .CommandTimeout(120) on the DbContext or query-level.
  2. Optimize the query by reducing the number of Includes, using explicit loading, or batching data retrieval with multiple queries.

无效尝试

常见但无效的做法:

  1. Removing AsSplitQuery() and relying on single query with joins, which may cause cartesian explosion 50% 失败

    Single queries with many Includes can result in huge result sets and memory pressure, leading to different performance issues.

  2. Setting CommandTimeout to a very high value (e.g., 5 minutes) globally in DbContext options 20% 失败

    This masks the underlying performance issue and may cause other queries to hang indefinitely.