70

The place where the command timeout is set is no longer the same as earlier versions.

However, I cannot find anywhere that says how to change this.

What I am doing is uploading very large files which takes longer than the default 30 seconds to save.

Note that I ask about Command Timeout, not Migration Timeout as in another question.

Ian Kemp
  • 26,561
  • 17
  • 107
  • 129
Greg Gum
  • 29,500
  • 31
  • 143
  • 205
  • 2
    Take a look at [How to set Entity Framework Core migration timeout?](http://stackoverflow.com/questions/39006847/how-to-set-entity-framework-core-migration-timeout) - the answer with `Database.SetCommandTimeout` – Ivan Stoev Aug 20 '16 at 20:50
  • Possible duplicate of [How to set Entity Framework Core migration timeout?](https://stackoverflow.com/questions/39006847/how-to-set-entity-framework-core-migration-timeout) – Mike Brind Apr 27 '18 at 14:36

5 Answers5

143

If you're using the DI container to manage the DbContext (i.e. you're adding the DbContext to the service collection), the command timeout can be specified in the options.

In Startup.ConfigureServices:

services.AddDbContext<YourDbContext>(options => options.UseSqlServer(
    this.Configuration.GetConnectionString("YourConnectionString"),
    sqlServerOptions => sqlServerOptions.CommandTimeout(60))
);
Massimiliano Kraus
  • 3,433
  • 5
  • 24
  • 45
Carl Sharman
  • 3,935
  • 1
  • 29
  • 29
  • 1
    This is most reasonable approach, uses built in configuration APIs, configuration is decoupled and easy to change, is part of class responsible for configiration. All other offets are sort of hacks. – Edgars Pivovarenoks Dec 30 '20 at 13:46
59

you can change it through your context

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext()
    {
        Database.SetCommandTimeout(150000);
    }
}
The Integrator
  • 1,784
  • 1
  • 10
  • 11
19

If you would like a temporary increase timeout only for one Context instance.

Let's say for 1 request (default Scoped context lifetime)

Change this before long running query:

Context.Database.SetCommandTimeout(TimeSpan.FromMinutes(20))

With scoped lifetime you can specify timeout only once and you do not have to specify it in any subsequent services constructors injections.

Oleg
  • 1,307
  • 4
  • 23
  • 39
  • Thanks for your answer which specifically addressed my concern about the lifetime of making such a change. However, I am curious as to how you know the value used in `SetCommandTimeout` is scoped only to the one context provided via DI and not to **every** instance of the context? Do you have a reference to documentation to support this? – Blair Allen Aug 24 '21 at 00:44
  • where should I put this line? Inside controllers IActionResult, inside using of dbContext statement, inside constructor of dbContext? – usernumber124153 Jan 24 '22 at 10:29
7

In EF Core 3 and above, you can now configure this via connection string. But you need to migrate from 'System.Data.SqlClient' to 'Microsoft.Data.SqlClient'.

Replace System.Data.SqlClient with Microsoft.Data.SqlClient version 2.1.0 or greater.

Then in your connection string simply append the command timeout like so:

"Data Source=SqlExpress;Initial Catalog=YourDatabase;Integrated Security=true;Command Timeout=300"

This will only work with Microsoft.Data.SqlClient 2.1.0 or above, you will get exception if you try this with System.Data.SqlClient.

Rosdi Kasim
  • 22,381
  • 23
  • 123
  • 149
6

The better option is to use CommandTimeout during your context setup like:

public class DbConnect: IConnnectDb
{
    private dbentitient _context;

    // inject this to a db entity from constructor. 

    //inside each method now use the follow before u actually run the query to db.  

    _context.Database.SetCommandTimeout(400);
}     

Note: EF Core will only execute the query with less than 100 seconds time. If it's more than that it keeps retrying and you never get to see the result.

That's my experience as of now, so let me know if you are able to fix it EF Core 1.0 does timeout even more fast than EF Core 2.0.

wonea
  • 4,425
  • 17
  • 82
  • 137
venkat
  • 179
  • 2
  • 7