title: Problems with EF7 connection on Azure App Service
body: <p>I am running an ASP.NET Core app with EF7 on an Azure App Service that has been running for 7 months. I recently upgraded from RC2 to 1.0 and since then, the app has started crashing after about 36-48 hours. Restarting the app temporarliy solves the problem.</p>
  
  <p>The response from the server is a blank 500-response (even if error pages are enabled).</p>
  
  <p>Looking through the logs I found the following errors in stdout:</p>
  
  <pre><code>fail: Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler[1]
    An exception occurred in the database while iterating the results of a query.
    System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed.
       at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task&amp; task, Boolean asyncWrite)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
       at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
       at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean openConnection, Boolean closeConnection)
       at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues, Boolean manageConnection)
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.MoveNext()
       at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.&lt;_ShapedQuery&gt;d__3`1.MoveNext()
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
       at lambda_method(Closure , QueryContext )
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.&lt;&gt;c__DisplayClass19_1`1.&lt;CompileQuery&gt;b__1(QueryContext qc)
  </code></pre>
  
  <p>followed by</p>
  
  <pre><code>fail: Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler[1]
    An exception occurred in the database while iterating the results of a query.
    System.InvalidOperationException: Internal connection fatal error. Error state: 15, Token : 16
       at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean&amp; dataReady)
       at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
       at System.Data.SqlClient.SqlDataReader.get_MetaData()
       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task&amp; task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task&amp; task, Boolean asyncWrite)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
       at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
       at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean openConnection, Boolean closeConnection)
       at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues, Boolean manageConnection)
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.MoveNext()
       at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.&lt;_ShapedQuery&gt;d__3`1.MoveNext()
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
       at lambda_method(Closure , QueryContext )
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.&lt;&gt;c__DisplayClass19_1`1.&lt;CompileQuery&gt;b__1(QueryContext qc)
  </code></pre>
  
  <p>This occurs at a step in the owin-pipeline that loads tenant data before the MVC framework is hooked in.</p>
  
  <p>I am using the built-in dependency injection to resolve my DbContext.</p>
  
  <p><a href="https://github.com/severisv/MyTeam/blob/master/src/MyTeam/Startup.cs" rel="nofollow">Startup.cs</a></p>
  
  <p><a href="https://github.com/severisv/MyTeam/blob/master/src/MyTeam/Services/Application/CacheHelper.cs#L77" rel="nofollow">Line where error occurs</a></p>