How can I set the command timeout of a DbContext?
You can use DbContext. Database. CommandTimeout = 180; It's pretty simple and no cast required.
Set database timeout in Entity Framework Try this on your context:...public class MyDatabase : DbContext { public MyDatabase () : base(ContextHelper. CreateConnection("Connection string"), true) { ((IObjectContextAdapter)this). ObjectContext. CommandTimeout = 180; } } ...
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.
I found this solution after another Google search. You can access the ObjectContext for a DbContext by casting this
to an IObjectContextAdapter.
From http://social.msdn.microsoft.com/Forums/en-ZA/adodotnetentityframework/thread/6fe91a64-0208-4ab8-8667-d061af340994:
public class MyContext : DbContext { public MyContext () : base(ContextHelper.CreateConnection("my connection string"), true) { ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 300; } }
A better solution to this for later versions of Entity Framework is to use the DbContext.Database.CommandTimeout
property. I think this came in with EF 6.
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