title: SqlException: Connection Timeout Expired in Startup.cs thrown immediately (without waiting for the timeout period)
body: <p>I have ASP.NET Core 2.2 MVC Web app, with Entity Framework and built-in identity provider.</p>
  
  <p>On application startup I access database (through the identity provider, which uses EF).</p>
  
  <p>When I open the website first time after longer period of inactivity, it takes ca 30 seconds and then I get an error:</p>
  
  <blockquote>
    <p>SqlException: Connection Timeout Expired</p>
  </blockquote>
  
  <p>That is ok, because I'm using Azure database with automatic shutdown on inactivity, so it probably takes longer time to start.</p>
  
  <p>But when I refresh the browser I get the timeout exception immediately, as if EF was trying to reuse the connection that just timed out. Can you explain why and how to fix it?</p>
  
  <pre><code>public virtual void ConfigureServices(IServiceCollection services)
  {
       services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt; options.UseSqlServer(Configuration.GetConnectionString("MyConnectionStr")),
           ServiceLifetime.Scoped, ServiceLifetime.Singleton);
  }
  
  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  {
      app.UseMvcWithDefaultRoute();
      app.UseStaticFiles();
  
      IServiceScopeFactory scopeFactory = app.ApplicationServices.GetRequiredService&lt;IServiceScopeFactory&gt;();
  
      using (IServiceScope scope = scopeFactory.CreateScope())
      {
          var serviceProvider = scope.ServiceProvider;
          var roleManager = serviceProvider.GetRequiredService&lt;RoleManager&lt;IdentityRole&gt;&gt;();
          var userManager = serviceProvider.GetRequiredService&lt;UserManager&lt;ApplicationUser&gt;&gt;();
          var logger = serviceProvider.GetRequiredService&lt;ILogger&lt;UserRolesConfig&gt;&gt;();
  
          roleManager.RoleExistsAsync(roleName).Wait(); //this throws the exception
      }
  }
  </code></pre>
  
  <p>This is the stacktrace:</p>
  
  <pre><code>System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, object providerInfo, string newPassword, SecureString newSecurePassword, bool redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, bool applyTransientFaultHandling, string accessToken)
  System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
  System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
  System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
  System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
  System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, uint waitForMultipleObjectsTimeout, bool allowCreate, bool onlyOneCheckConnection, DbConnectionOptions userOptions, out DbConnectionInternal connection)
  System.Data.ProviderBase.DbConnectionPool.WaitForPendingOpen()
  Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnectionAsync(bool errorsExpected, CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, bool errorsExpected)
  Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable&lt;T&gt;+AsyncEnumerator.BufferlessMoveNext(DbContext _, bool buffer, CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync&lt;TState, TResult&gt;(TState state, Func&lt;DbContext, TState, CancellationToken, Task&lt;TResult&gt;&gt; operation, Func&lt;DbContext, TState, CancellationToken, Task&lt;ExecutionResult&lt;TResult&gt;&gt;&gt; verifySucceeded, CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable&lt;T&gt;+AsyncEnumerator.MoveNext(CancellationToken cancellationToken)
  System.Linq.AsyncEnumerable.FirstOrDefault_&lt;TSource&gt;(IAsyncEnumerable&lt;TSource&gt; source, CancellationToken cancellationToken) in First.cs
  Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider+TaskResultAsyncEnumerable&lt;T&gt;+Enumerator.MoveNext(CancellationToken cancellationToken)
  System.Linq.AsyncEnumerable+SelectEnumerableAsyncIterator&lt;TSource, TResult&gt;.MoveNextCore(CancellationToken cancellationToken) in Select.cs
  System.Linq.AsyncEnumerable+AsyncIterator&lt;TSource&gt;.MoveNext(CancellationToken cancellationToken) in AsyncIterator.cs
  Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider+ExceptionInterceptor&lt;T&gt;+EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteSingletonAsyncQuery&lt;TResult&gt;(QueryContext queryContext, Func&lt;QueryContext, IAsyncEnumerable&lt;TResult&gt;&gt; compiledQuery, IDiagnosticsLogger&lt;Query&gt; logger, Type contextType)
  Microsoft.AspNetCore.Identity.RoleManager&lt;TRole&gt;.RoleExistsAsync(string roleName)
  </code></pre>