title: DbContext and Connection pools
body: <p>In an application that I've inherited there's this in a Base Controller, that every other controller in the application inherits from.</p>
  
  <pre><code>public BaseController()
      {
          db = new MyDbContext();
  
          db.Database.Log = s =&gt; Debug.Write(s);
      }
  
   public MyDbContext()
          : base("name=MyDbContext")
      {
          // hack to force Visual Studio to deploy the Entityframework.SqlServer package 
          var instance = SqlProviderServices.Instance;
      }
  </code></pre>
  
  <p>Due to the way the application has been designed, at least 2 contexts are created per request. (It's an MVC application and there is a call to the HomeController on every page plus whatever other controllers are called for a particular page.)</p>
  
  <p>My question is when does the <code>DbContext</code> create a connection to SQL Server? Is it immediately when the context is created, or only when a query is executed?</p>
  
  <p>If it's the former, then i will be using 2 twice the number of connections to SQL server than is needed, and if it's the latter then it's probably not too much of an issue.</p>
  
  <p>I don't think i can refactor this in the immediate future, certainly not without justification. What potential pitfalls of this design should i be aware of?</p>
  
  <p>Entity Framework 6.1.3</p>