Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Entity Framework Core migration timeout?

I'm using the latest (1.0.0) version of EF Core. I have a migration to run on a quite big database.

I run:

dotnet ef database update -c ApplicationDbContext

And get:

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

In the connection string I explicitly set the timeout like so:

Connect Timeout=150000

Unfortunately, it didn't help. How should I do this?

like image 541
Andrzej Gis Avatar asked Aug 17 '16 21:08

Andrzej Gis


People also ask

How do I set the execution timeout in Entity Framework?

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; } } ...

What is default timeout in Entity Framework?

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.


2 Answers

The error message you are getting is for a Command timeout, not a connection timeout.

UPDATE

As mentioned by Pace in comments, since EF Core 2.0 you are able to use IDesignTimeDbContextFactory to change the behaviour of your context when it is being created by tooling at design time such as happens with Migrations.

Create a separate class in your project that implements the IDesignTimeDbContextFactory interface and use the DbContextoptionsBuilder to configure the behaviour you want - in this case, setting the command timeout value to 600 seconds:

using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design;  namespace EFCoreSample.Model {     public class SampleContextFactory : IDesignTimeDbContextFactory<SampleContext>     {         public SampleContext CreateDbContext(string[] args)         {             var optionsBuilder = new DbContextOptionsBuilder<SampleContext>();             optionsBuilder.UseSqlServer(@"Server=.\;Database=db;Trusted_Connection=True;",                 opts => opts.CommandTimeout((int)TimeSpan.FromMinutes(10).TotalSeconds));              return new SampleContext(optionsBuilder.Options);         }     } } 

Make sure that your existing DbContext has a constructor that takes a DbContextOptions object as a parameter:

public AdventureContext(DbContextOptions options) : base(options){} 

When the tooling runs the migration, it looks first for a class that implements IDesignTimeDbContextFactory and if found, will use that for configuring the context. Runtime behaviour is not affected.

Original Answer No Longer Applies

There is no way to set the CommandTimeout on a context when using EF commands. But you can set it globally in the constructor, and then remove it later if you don't need to keep it:

public class ApplicationDbContext : DbContext {     public ApplicationDbContext()     {         Database.SetCommandTimeout(150000);     } } 
like image 189
Mike Brind Avatar answered Sep 23 '22 06:09

Mike Brind


You can set the timeout for migration only by setting the timeout on the context before calling the Migrations method:

using (var context = new DispatchingDbContext(_configuration)) {     context.Database.SetCommandTimeout(300);     await context.Database.MigrateAsync().ConfigureAwait(false); } 

Set timeout for migrations ef .netcore

like image 30
Peter Avatar answered Sep 24 '22 06:09

Peter