title: EF4 Code-First causes InvalidOperationException
body: <p>I'm having an issue when trying to run my project each time it builds. It seems the initializer runs, but when it comes to the first query - it dies with the following <code>InvalidOperationException</code>.</p>
  
  <pre><code>This operation requires a connection to the 'master' database. Unable to create a
  connection to the 'master' database because the original database connection has
  been opened and credentials have been removed from the connection string. Supply
  an unopened connection.
  </code></pre>
  
  <p>For reference, I'm using the EF Code First CTP4, imported directly with NuGet. Connecting to a SQL Server 2008 R2</p>
  
  <p>What I want to happen is to re-create the database if there are any model amendments and seed it with a few values for the lookup table. Both of these things seem to be supported* out of the box.</p>
  
  <p>My setup is like so:</p>
  
  <p>Global.asax</p>
  
  <pre><code> protected void Application_Start()
   {
      Database.SetInitializer&lt;CoreDB&gt;(new CoreDBInitialiser());
      // et al...
   }
  </code></pre>
  
  <p>CoreDB.cs</p>
  
  <pre><code>public class CoreDB : DbContext
  {
      public DbSet&lt;User&gt; Users { get; set; }
      public DbSet&lt;Login&gt; Logins { get; set; }
      public DbSet&lt;Permission&gt; Permissions { get; set; }
      public DbSet&lt;Role&gt; Roles { get; set; }
      public DbSet&lt;RolePermission&gt; RolePermissions { get; set; }
      public DbSet&lt;UserRole&gt; UserRoles { get; set; }
      public DbSet&lt;Setting&gt; Settings { get; set; }
  }
  
  public class CoreDBInitialiser : RecreateDatabaseIfModelChanges&lt;CoreDB&gt;
  {
      protected override void Seed(CoreDB context)
      {
          var settings = new List&lt;Setting&gt;
          {
              new Setting
              {
                  SettingName = "ExampleSetting",
                  SettingValue = "This is a sample setting value",
              }
          };
  
          settings.ForEach(d =&gt; context.Settings.Add(d));
      }
  }
  </code></pre>
  
  <p>When it runs, it dies on a line similar to this, which is basically the first query it comes across after creating the database.</p>
  
  <pre><code>User data = (from u in _data.Users where u.Username == userName  select u).SingleOrDefault();
  </code></pre>
  
  <p>Things I don't think it is:</p>
  
  <ul>
  <li><strong>It's not permissions</strong>: I've deleted the actual database itself within the SQL Server. The application recreates it around about the same time as that query is attempted to run (the initializer is set, then obviously it holds off creating until it's needed). I've also logged on to SQL Server as the user that is specified in my Web.config and they have full read/write access to the database. In fact, they probably should do as that account creates the databases also.</li>
  <li><strong>The database is being created</strong>: Deleting the DB and it automatically recreates fine.</li>
  <li><strong>The connection string is correctly defined</strong>, including the <code>providerName</code> attribute.</li>
  </ul>
  
  <p><code>&lt;add name="CoreDB" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=TheDatabase;User Id=TheUsername;Password=ThePassword;" providerName="System.Data.SqlClient" /&gt;</code></p>
  
  <ul>
  <li>It doesn't <em>appear</em> to be a bug in my code/logic, as once the query has failed successfully, <strong>the code will start properly until the next time the application is rebuilt</strong>. It's obviously could be, and likely I'd have to apply a work around in my code no matter what anyway. :)</li>
  </ul>
  
  <p><strong>What to do?</strong></p>
  
  <p>Ideally, I'd like to "not think about the database schema" much. I'd like it to be as it seemed to be in <a href="http://weblogs.asp.net/scottgu/archive/2010/07/23/entity-framework-4-code-first-custom-database-schema-mapping.aspx#7573964" rel="noreferrer">Scott Gu's excellent blog post</a> (and follow up for <a href="http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx" rel="noreferrer">working with existing databases</a>) where things just worked out and disappeared away. For the most part this is true. It seems to be an issue with the connection not being closed at some point, but I can't find out how to rectify this issue.</p>
  
  <p>A <a href="http://social.msdn.microsoft.com/Forums/en-NZ/adonetefx/thread/c5329e4b-0efb-4951-85b1-0a123579cc3d" rel="noreferrer">few forum</a> / SO posts do imply the issue I'm having is basically because the initializers aren't working exactly as planned and the connection might be left open. The solution in other places appears to be to simply "don't create your own initializer", which isn't the greatest solution - but unless anyone has any ideas, I'll probably have to do until CTP5 maybe.</p>
  
  <p>*yep, I know it's a CTP, so "supported" is probably not the word :)</p>