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.
Popular Answer You can use DbContext. Database. CommandTimeout = 180; It's pretty simple and no cast required.
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.
The time in seconds to wait for the command to execute.
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; } }
var ctx = new DbContext(); ctx.Database.CommandTimeout = 120;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With