Azure SQL Database Timeouts in Entity Framework 6 Code First Migrations

Entity Framework 6 has some surprising places to set database timeouts with code-first migrations, especially using Azure SQL

Sean Fisher

3 minute read

Azure SQL Database Timeouts in Entity Framework 6 Code First Migrations

Recently we ran into an issue with EF 6. As part of our continuous integration/continuous delivery pipeline we run integration tests in one of our environments. In order to achieve exactly reproducible (and fast) test runs, we spin up four fresh databases and add them to a pool. We initialize the database using EF (so it runs our migrations), perform the tests, and then tear them down.

We were trying to initialize a new database with code like this:

var connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
using (var myContext = new MyContext(connectionString))
{
    myContext.Database.Initialize(true);
}

We kept getting an error when Entity Framework would try to create the database in Azure SQL:

OneTimeSetUp: System.AggregateException : One or more errors occurred.
----> System.Data.SqlClient.SqlException : Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
CREATE DATABASE operation failed. Internal service error.
----> System.Data.SqlClient.SqlException : Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
CREATE DATABASE operation failed. Internal service error.
----> System.Data.SqlClient.SqlException : Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
CREATE DATABASE operation failed. Internal service error.
----> System.Data.SqlClient.SqlException : Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
----> System.ComponentModel.Win32Exception : The wait operation timed out
----> System.ComponentModel.Win32Exception : The wait operation timed out
----> System.ComponentModel.Win32Exception : The wait operation timed out
----> System.ComponentModel.Win32Exception : The wait operation timed out

And, of course, it worked just fine when running locally against a local database. It was only in Azure SQL that we experienced the issue.

No problem, right? We’ll just up the timeout value. We’ll do it in two places.

1) The command timeout in the constructor of the context:

public class MyContext : DbContext, IMyContext
{
    public MyContext(string connectionString) : base(connectionString)
    {
        Database.CommandTimeout = 900;
    }
}

2) The connection timeout in the connection string:

var connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
var csBuilder = new SqlConnectionStringBuilder(connectionString)
{
    ConnectTimeout = 900
};
using (var myContext = new MyContext(csBuilder.ConnectionString))
{
    myContext.Database.Initialize(true);
}

However, this still didn’t work. After much hair-pulling I stumbled upon this MSDN article that mentions that EF Migrations pull their timeout from a separate configuration. All we had to do to fix the issue was change the timeout in the right place:

public class MyContextConfiguration : DbMigrationsConfiguration<MyContext>
{
    public MyContextConfiguration()
    {
        CommandTimeout = 900;
    }
}

And it started working again.

comments powered by Disqus