title: "The underlying provider failed on Open" Entity Framework error
body: <p>I've been getting the error "The underlying provider failed on Open." recently and I've been pulling my hair out trying to figure out why.  On closer inspection of the error the inner exception is "The connection was not closed. The connection's current state is connecting." and the stack trace is </p>
  
  <pre><code>"at System.Data.ProviderBase.DbConnectionClosedConnecting.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
     at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
     at System.Data.SqlClient.SqlConnection.Open()
     at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean&amp; closeStoreConnectionOnFailure)"
  </code></pre>
  
  <p>As far as I can tell the error seems to happen at random or possibly when making changes to the application.  Any help would be greatly appreciated.</p>
  
  <p>My current setup is</p>
  
  <ul>
  <li>IIS Express</li>
  <li>Microsoft SQL Server Express Edition</li>
  <li>Visual Studio 2012</li>
  <li>MVC Web API \ Asp.net MVC 4</li>
  <li>Entity Framework 5</li>
  </ul>
  
  <p>My data layer is modeled off the Repository pattern and injected via Structure map.</p>
  
  <p><strong>Repository</strong></p>
  
  <pre><code>public class Repository&lt;T&gt; : IDisposable, IRepository&lt;T&gt; where T : class
  {
      private readonly DbSet&lt;T&gt; _dbSet;
      private readonly DbContext _dbContext;
  
      public Repository(IDbContextFactory&lt;MyContext&gt; dbContextFactory)
      {
          _dbContext = dbContextFactory.Create();
          _dbSet = _dbContext.Set&lt;T&gt;();
      }
  
      //removed for brevitity
  
      public void Dispose()
      {
          _dbContext.Dispose();
      }
  </code></pre>
  
  <p><strong>DbContextFactory</strong></p>
  
  <pre><code>public class MyContextFactor : IDbContextFactory&lt;MyContext&gt;, IDisposable
  {
      private MyContext _dataContext;
  
      public MyContextFactor Create()
      {
          return _dataContext ?? (_dataContext = new MyContext());
      }
  
      public void Dispose()
      {
          _dataContext.Dispose();
      }
  }
  </code></pre>
  
  <p><strong>Structure Map Registry for IOC</strong></p>
  
  <pre><code>For(typeof(IDbContextFactory&lt;MyContext&gt;)).HybridHttpOrThreadLocalScoped().Use(typeof(MyContextFactory));
  </code></pre>
  
  <p><strong>Connection String</strong></p>
  
  <pre><code>&lt;add name="MyContextConnectionString" connectionString="metadata=res://*/Model.MyProject.csdl|res://*/Model.MyProject.ssdl|res://*/Model.MyProject.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=(local);initial catalog=MyDB;User Id=SQLUser;Password=****;MultipleActiveResultSets=True;App=EntityFramework&amp;quot;" providerName="System.Data.EntityClient"/&gt;
  </code></pre>
  
  <p>Things I've attempted to fix the problem and so far haven't worked</p>
  
  <ul>
  <li>ensure context is per http context via structure map scope</li>
  <li>added dispose to repository</li>
  <li>added dispose to ContextFactory</li>
  <li>switched from Intergrated Sql auth to Sql authentication</li>
  </ul>