title: EF Code First - Timeout expired. The timeout period elapsed prior to completion
body: <p>Apology for this strangely worded question. I don't know what the actual problem is but hopefully someone can give me some insights.</p>
  
  <p>I am getting the following error when trying to run migrations:</p>
  
  <pre><code>Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---&gt; System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out
  </code></pre>
  
  <p>It is interesting to note that on my laptop this is not happening but on my VM (azure - large) this is happening with 100% failure rate.</p>
  
  <p>I am using <strong>Ef 6.0.0 -rc1</strong>. Please note, updating EF is not an option. If updating to EF 6.0.0 or 6.0.1 I will get the following error with 100% failure rate:</p>
  
  <p><a href="https://stackoverflow.com/questions/18900204/">Errors during Code First add-migration</a></p>
  
  <p>I have also timed the error. It takes about 1.5 min to trigger the error. When running with <code>-Verbose</code> flag it was trying to create 200 tables with indexes. Copying the sql query and excuting it in SSMS takes 5 secs.</p>
  
  <p>A few things that I have tired but didn't work:</p>
  
  <p>1) Setting <code>ObjectContext.CommandTimeout = 36000 // 10 hours!</code> as indicated here:</p>
  
  <p><a href="https://stackoverflow.com/a/6234593/305469">https://stackoverflow.com/a/6234593/305469</a></p>
  
  <p>2) Setting timeout in connection string in "web.config":</p>
  
  <p><code>
  connectionString="Data Source=localhost;Initial Catalog=myDB;Integrated Security=SSPI;Connection Timeout=36000"
  </code></p>
  
  <p>3) Setting "machine.config" transaction maxTimeout:</p>
  
  <p><code>
      &lt;system.transactions&gt;
          &lt;machineSettings maxTimeout="00:00:00" /&gt;
      &lt;/system.transactions&gt;
  </code></p>
  
  <p>4) Setting "remote query timeout" on sql server</p>
  
  <pre><code>USE MyDB;
  GO
  EXEC sp_configure 'remote query timeout', 0 ;
  GO
  RECONFIGURE ;
  GO
  </code></pre>
  
  <p>So what is happening? How come <code>CommandTimeout</code> is not being respected? Any suggestions?</p>
  
  <p>Full trace as follows:</p>
  
  <pre><code>System.Data.SqlClient.SqlException (0x80131904): Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---&gt; System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out
  
     at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
     at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
     at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
     at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean&amp; dataReady)
     at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout)
     at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
     at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
     at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TInterceptionContext,TResult](Func`1 operation, TInterceptionContext interceptionContext, Action`1 executing, Action`1 executed)
     at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
     at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
     at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
     at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection)
     at System.Data.Entity.Migrations.DbMigrator.&lt;&gt;c__DisplayClass32.&lt;ExecuteStatements&gt;b__2e()
     at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.&lt;&gt;c__DisplayClass1.&lt;Execute&gt;b__0()
     at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
     at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
     at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable`1 operations, IEnumerable`1 systemOperations, Boolean downgrading, Boolean auto)
     at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
     at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
     at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
     at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
     at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
     at System.Data.Entity.Migrations.DbMigrator.&lt;&gt;c__DisplayClassc.&lt;Update&gt;b__b()
     at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
     at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
     at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
     at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
     at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
     at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
     at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
     at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
     at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
     at System.Data.Entity.Migrations.UpdateDatabaseCommand.&lt;&gt;c__DisplayClass2.&lt;.ctor&gt;b__0()
     at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
  ClientConnectionId:8cbbc70c-8182-417e-9aca-4603f797340d
  Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
  </code></pre>