Set database timeout in Entity Framework
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Entity Framework, "database timeout" can mean two different things: how long the client waits to establish a connection, and how long it waits for a SQL command to finish. Those settings live in different places, and confusing them is a common reason timeout fixes appear to do nothing.
Connection Timeout vs Command Timeout
The connection timeout is part of the connection string. It affects how long the provider waits while opening the database connection.
The command timeout affects how long EF allows an individual query or command to run after the connection is already open.
If a query times out after thirty seconds, changing the connection string may not help. You probably need to change the command timeout instead.
Entity Framework 6: Set Database.CommandTimeout
In EF6, the usual way to change command timeout is through the context:
The value is in seconds. This affects commands executed through that context instance.
If you need a global default for a specific context type, set it in the constructor:
That is often the cleanest approach when the same timeout rule should apply to the whole application.
Entity Framework Core: Use SetCommandTimeout
In EF Core, the usual pattern is:
You can also configure the timeout when registering the provider:
This is useful when you want the timeout policy centralized in application startup rather than repeated in query code.
Connection Timeout Lives in the Connection String
If the issue is opening the connection itself, change the connection string instead:
This setting is separate from command timeout. It helps when the network or SQL Server instance is slow to accept the connection, but it will not make a long-running query continue longer once execution has begun.
Do Not Use Large Timeouts to Hide Slow Queries
Raising a timeout can be the right tactical fix for a known long-running operation such as a migration, report, or bulk import. It should not be the default response to every timeout exception.
If normal application queries keep timing out, the real issue may be:
- missing indexes
- blocking or deadlocks
- inefficient LINQ translation
- fetching too much data
In those cases, a bigger timeout only delays the error and makes the user wait longer.
Common Pitfalls
The biggest mistake is changing the connection string when the failure is really a command timeout. Those are different settings and solve different problems.
Another common issue is setting the timeout on one context instance and expecting it to affect all future contexts automatically. In EF, that only happens if you centralize the configuration yourself.
It is also easy to treat timeouts as a database-performance strategy. A timeout value is a safety boundary, not a substitute for query tuning and indexing.
Summary
- Entity Framework timeout configuration depends on whether the problem is connection opening or command execution.
- In EF6, use
Database.CommandTimeoutfor per-context command timeout control. - In EF Core, use
Database.SetCommandTimeoutor provider configuration during startup. - Use
Connect Timeoutin the connection string only for connection-establishment delays. - Increase timeouts deliberately, but fix slow queries instead of masking them permanently.

