Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework not respecting Command Timeout

I ran into an issue with my entity framework based api where 3rd party developers were mistakenly sending queries that were way too large and caused the system to drop performance. I've informed them to stop the practice but I would like to limit queries to 1 minute and then just cut them off.

It sounds like I should be able to just set the command timeout in the constructor (shown below). When I test it out with a long query it performs the query exactly as it was before (3+ minutes), it doesn't seem to be respecting the command timeout at all.

Did I do something wrong? Is this not how the command timeout is expected to work? It is async, does the command timeout not work with async? Any solutions or pointers would be greatly appreciated.

public class CustomContext : DbContext
{
    public CustomContext(string connectionName)
        : base(connectionName)
    {
        var objectContext = (this as IObjectContextAdapter).ObjectContext;
        objectContext.CommandTimeout = 60;
    }

    public CustomContext(EntityConnection connection)
        : base(connection, contextOwnsConnection: false)
    {
        var objectContext = (this as IObjectContextAdapter).ObjectContext;
        objectContext.CommandTimeout = 60;
    }
}
like image 968
Jacob C. Voller Avatar asked Jul 05 '26 06:07

Jacob C. Voller


1 Answers

Check this :

Entity Framework 6:

this.context.Database.CommandTimeout = 180;

Entity Framework 5:

((IObjectContextAdapter)this.context).ObjectContext.CommandTimeout = 180;

Entity Framework 4 and below:

this.context.CommandTimeout = 180;
like image 157
danghyan Avatar answered Jul 06 '26 19:07

danghyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!