My command keeps timing out, so I need to change the default command timeout value.
I've found myDb.Database.Connection.ConnectionTimeout
, but it's readonly
.
How can I set the command timeout in Entity Framework 5 ?
My command keeps timing out, so I need to change the default command timeout value.
I've found myDb.Database.Connection.ConnectionTimeout
, but it's readonly
.
How can I set the command timeout in Entity Framework 5 ?
Try this on your context:
If you want to define the timeout in the connection string, use the
Connection Timeout
parameter like in the following connection string:Source: How to: Define the Connection String
You can use
DbContext.Database.CommandTimeout = 180;
It's pretty simple and no cast required.
I extended Ronnie's answer with a fluent implementation so you can use it like so:
dm.Context.SetCommandTimeout(120).Database.SqlQuery...
My partial context looks like:
I left
SetCommandTimeOut
public so only the routines I need to take a long time (more than 5 minutes) I modify instead of a global timeout.In the generated constructor code it should call
OnContextCreated()
I added this partial class to solve the problem:
For Database first Aproach:
We can still set it in a constructor, by override the ContextName.Context.tt T4 Template this way:
Database.CommandTimeout = 180;
is the acutaly change.The generated output is this:
If you change your Database Model, this template stays, but the actualy class will be updated.