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

- **ID:** `dotnet/ef-core-query-split-query-timeout`
- **领域:** dotnet
- **类别:** data_error
- **错误码:** `0x80131904`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| EF Core 5.0 | active | — | — |
| EF Core 6.0 | active | — | — |
| EF Core 7.0 | active | — | — |
| EF Core 8.0 | active | — | — |
| SQL Server 2016+ | active | — | — |

## 解决方案

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.
   ```

## 无效尝试

- **Removing AsSplitQuery() and relying on single query with joins, which may cause cartesian explosion** — Single queries with many Includes can result in huge result sets and memory pressure, leading to different performance issues. (50% 失败率)
- **Setting CommandTimeout to a very high value (e.g., 5 minutes) globally in DbContext options** — This masks the underlying performance issue and may cause other queries to hang indefinitely. (20% 失败率)
