# 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-sql-timeout`
- **Domain:** dotnet
- **Category:** data_error
- **Error Code:** `-2146232060`
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

Entity Framework Core query or command exceeds the default 30-second command timeout due to complex joins, unindexed queries, or large result sets in SQL Server.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| .NET 6.0 | active | — | — |
| .NET 7.0 | active | — | — |
| .NET 8.0 | active | — | — |
| EF Core 6.0 | active | — | — |
| EF Core 7.0 | active | — | — |
| EF Core 8.0 | active | — | — |

## Workarounds

1. **Add database indexes on columns used in WHERE and JOIN clauses. Example: CREATE INDEX IX_Orders_CustomerId ON Orders (CustomerId);** (90% success)
   ```
   Add database indexes on columns used in WHERE and JOIN clauses. Example: CREATE INDEX IX_Orders_CustomerId ON Orders (CustomerId);
   ```
2. **Set CommandTimeout to a higher value (e.g., 120 seconds) in OnConfiguring: optionsBuilder.UseSqlServer(connectionString, opts => opts.CommandTimeout(120));** (75% success)
   ```
   Set CommandTimeout to a higher value (e.g., 120 seconds) in OnConfiguring: optionsBuilder.UseSqlServer(connectionString, opts => opts.CommandTimeout(120));
   ```
3. **Optimize LINQ query by using .AsSplitQuery() to avoid Cartesian explosion: context.Orders.Include(o => o.Details).AsSplitQuery().ToList();** (85% success)
   ```
   Optimize LINQ query by using .AsSplitQuery() to avoid Cartesian explosion: context.Orders.Include(o => o.Details).AsSplitQuery().ToList();
   ```

## Dead Ends

- **Set CommandTimeout to 0 (infinite) in DbContext options** — Infinite timeout can cause indefinite hangs and resource exhaustion; it masks the underlying query performance issue. (70% fail)
- **Add .AsNoTracking() to all queries** — AsNoTracking reduces overhead but does not fix slow queries; timeout still occurs if the query is slow due to missing indexes. (85% fail)
- **Increase SqlCommand timeout in appsettings** — Increasing timeout without optimizing the query or adding indexes only delays the failure; the query may still fail under load. (60% fail)
