4

I am currently experiencing a SQL Timeout when running a SQL() command inside of an EF Migration.

Situation: I am replacing a bunch (>50) tables with one table, and need to convert the data from those tables I'm about to drop into the new table. I've organized the Migration the following way:

1.Create the new table.

  1. In the same Migration, use the SQL() function to run a sql script that migrates the data.

3.Drop all the old tables.

Currently, the migration gives the following error:

System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

The error is happening in an environment where I give them an installer, and they run it without me involved, so I can't manually run individual migrations, and pause in the middle to run the SQL script.

Is there any way to change the timeout for a connection, or get around this issue?

Environment:

EF 6.0 Code First

SQL Server 2012

Josh Hanks
  • 111
  • 1
  • 4

2 Answers2

7

See this answer.

Use Configuration.cs file to set custom time out:

internal sealed class Configuration :

DbMigrationsConfiguration<ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        ContextKey = "YourDbContext";

        // New timeout in seconds
        this.CommandTimeout = 60 * 5; 
    }
}

With this method, you can change the timeout only for migration and not everyone using your default connection string.

Community
  • 1
  • 1
AXMIM
  • 2,424
  • 1
  • 20
  • 38
0

You should be able to set the connection timeout in the connection string, something like:

Connection Timeout=180;
illug
  • 793
  • 1
  • 9
  • 23
  • Can't we just change the timeout just for the migration? Changing it in the connection string will change it for users of the app as well and I don't want that. – AXMIM Aug 16 '16 at 14:20
  • Found out that you can mannually overwrite the connection string when using "Package Manager Console", but didn't how it could be used for automatic migration. – AXMIM Aug 16 '16 at 14:43
  • 2
    -1 as this is the "Connection Timeout", whereas the timeout in the question is a "Command Timeout" which isn't part of the connection string – thab Jul 16 '19 at 11:54