Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set CommandTimeout for DbContext?

I am looking a way to set CommandTimeout for DbContext. After searching I found the way by casting DbContext into ObjectContext and setting value for CommandTimeout property of objectContext.

var objectContext = (this.DbContext as IObjectContextAdapter).ObjectContext; 

But I have to work with DbContext.

like image 779
Yara Avatar asked May 11 '12 10:05

Yara


People also ask

How do I set the execution timeout in Entity Framework?

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

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.

What is CommandTimeout in connection string?

The time in seconds to wait for the command to execute.


2 Answers

It will work with your method.

Or subclass it (from msdn forum)

public class YourContext : DbContext {   public YourContext()     : base("YourConnectionString")   {     // Get the ObjectContext related to this DbContext     var objectContext = (this as IObjectContextAdapter).ObjectContext;      // Sets the command timeout for all the commands     objectContext.CommandTimeout = 120;   } } 
like image 111
Jonas Lincoln Avatar answered Sep 29 '22 22:09

Jonas Lincoln


var ctx = new DbContext(); ctx.Database.CommandTimeout = 120; 
like image 30
Perry Tribolet Avatar answered Sep 30 '22 00:09

Perry Tribolet