Extending the Command Timeout in Entity Framework Core Migrations

5 (1 vote)

Most migrations that you execute when working with Entity Framework Core are likely to be straightforward - resulting in nothing more than an adjustment to the database schema. Sometimes, however, you might also want to include a task that requires longer than the default command timeout value (30 seconds in SQL Server) such as importing a lot of data.

Prior to version 2.0 of EF Core, your options were limited to changing the command timeout for the DbContext, running the migration and then resetting the timeout value (or not):

public SampleContext()
{
    Database.SetCommandTimeout((int)TimeSpan.FromMinutes(5).TotalSeconds);
}

The IDesignTimeDbContextFactory was introduced in EF Core 2.0 to alleviate this (and other) problems associated with configuring DbContext objects differently for certain design-time tasks, such as migrations.

This interface is only intended for use with design time tools such as migrations. The tools are configured to search the assembly containing the DbContext (or the one designated as the startup assembly) for a type that implements the interface. If one exists, it is instantiated and its CreateDbContext method is called which returns the derived DbContext - enabling you to configure the context in a different manner to the way it is configured for runtime use. As such, it provides a hook for overriding the construction of the DbContext type.

The following example illustrates the use of IDesignTimeDbContextFactory to override the creation of SampleContext, specifying a command timeout value of 10 minutes, as opposed to the default 30 seconds, or whatever value has been specified for the runtime version of the context:

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

Finally, you must ensure that your context has a constructor that takes a DbContextOptions object as a parameter:

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

It should be noted at this stage that the string[] args parameter on the CreateDbContext method is not currently implemented. Decisions have yet to be made on how arguments can be passed to this method via the various design-time tools that discover types that implement the interface.

 

Date Posted:
Last Updated:
Posted by:
Total Views to date: 471

0 Comments

Recent Comments

Thomas 05/03/2018 00:59
In response to I'm Not Writing A Book On Razor Pages
There's a typo on this page: = true)] should be [BindProperty(SupportsGet = true)]...

Rolf Herbert 04/03/2018 19:25
In response to I'm Not Writing A Book On Razor Pages
So is MS deprecating razor Web Pages..? Is it dead..? I wish they would stop killing things so its...

Borut 17/02/2018 12:59
In response to I'm Not Writing A Book On Razor Pages
Mike, is it possible that Web Pages and Razor Pages "live" together in one web application? I a I...

hrboyce 09/02/2018 04:44
In response to I'm Not Writing A Book On Razor Pages
Mike, First thanks for doing this but I have to ask, any chance you would consider converting one of...

aziz sallam 07/02/2018 10:18
In response to I'm Not Writing A Book On Razor Pages
u are a great man...

Satyabrata Mohapatra 31/01/2018 11:36
In response to I'm Not Writing A Book On Razor Pages
This is a great news!!!! Thanks...

tangdf 30/01/2018 07:25
In response to I'm Not Writing A Book On Razor Pages
=> { o.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute()); }); The extension method does...

Obinna Okafor 30/01/2018 04:02
In response to I'm Not Writing A Book On Razor Pages
Thank you very much. I would like to see a project built from scratch using Razor Pages. And it show...

rachida Dukes 31/10/2017 13:52
In response to Customising Identity in Razor Pages
Thanks again for this wonderful tutorial. I followed all the steps in this section called: Adding...

Rachida 31/10/2017 12:06
In response to Customising Identity in Razor Pages
Thanks very much for this wonderful tutorial, it helped a lot....