Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you increase timeout in Linq2Entities?

I'm doing a basic select against a view. Unfortunately the result can be slow and I'm getting timeout errors intermittently. How can I increase the timeout?

Using .NET 3.5, Sql Server 2000, Linq2Entities

I'm using the very basic query List<MyData> result = db.MyData.Where(x.Attribute == search).ToList();

Fixing the query so that it's faster on the DB side is not an option here.

Exact Error: "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."

Update: I'd prefer to just change it for this one query.

like image 715
Russell Steen Avatar asked Mar 16 '10 12:03

Russell Steen


People also ask

How do I increase timeout in Entity Framework?

You can use DbContext. Database. CommandTimeout = 180; It's pretty simple and no cast required.

How to set timeout in DbContext?

public partial class MyEntities : DbContext { public MyEntities() : base("name=MyConnectionStringName") { } ... } In your code, instantiate the Container class and if you need to use a custom timeout for a specific command, set it manually using the provided methods. using (var db = new MyEntitiesContainer()) { db.

What is default timeout in Entity Framework?

The timeout period elapsed prior to completion of the operation or the server is not responding.” A Solution: As near as I can find, the default timeout for EF queries using the SQL Server 2008 connection provider is 30 seconds.


1 Answers

You can set the timeout in your connection string.

Edit (new): It turns out that there are two different timeout concepts. The connection timeout is used to determine wether or not a connection can be established. The CommandTimeout property on the object context controls timeout for commands. So just set that to a high value, and it should not influence the short-running queries in any way.

like image 198
Klaus Byskov Pedersen Avatar answered Nov 05 '22 20:11

Klaus Byskov Pedersen