I am getting an problem when attempting to operate my project every time it develops. It appears the initializer runs, however when it involves the very first query - it dies using the following InvalidOperationException.

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.

For reference, I am while using EF Code First CTP4, imported directly with NuGet. Hooking up to some SQL Server 2008 R2

What I wish to happen would be to re-produce the database if you will find any model changes and seed it having a couple of values for that research table. These two things appear to become supported* as they are.

My setup is much like so:

Global.asax

 protected void Application_Start()
 {
    Database.SetInitializer<CoreDB>(new CoreDBInitialiser());
    // et al...
 }

CoreDB.cs

public class CoreDB : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Login> Logins { get; set; }
    public DbSet<Permission> Permissions { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<RolePermission> RolePermissions { get; set; }
    public DbSet<UserRole> UserRoles { get; set; }
    public DbSet<Setting> Settings { get; set; }
}

public class CoreDBInitialiser : RecreateDatabaseIfModelChanges<CoreDB>
{
    protected override void Seed(CoreDB context)
    {
        var settings = new List<Setting>
        {
            new Setting
            {
                SettingName = "ExampleSetting",
                SettingValue = "This is a sample setting value",
            }
        };

        settings.ForEach(d => context.Settings.Add(d));
    }
}

If this runs, it dies on the line such as this, that is essentially the very first query it results in after creating the database.

User data = (from u in _data.Users where u.Username == userName  select u).SingleOrDefault();

A Few Things I don't believe it is:

  • It isn't permissions: I have erased the particular database itself inside the SQL Server. The applying recreates it around comparable time as that question is tried to run (the initializer is placed, then clearly it holds off creating until it's needed). I have also drenched onto SQL Server because the user that's specified by my Web.config and they've full read/write use of the database. Actually, they most likely must do as that account produces the databases also.
  • The database has been produced: Removing the DB also it instantly recreates fine.
  • The bond string is properly defined, such as the providerName attribute.

<add name="CoreDB" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=TheDatabase;User Id=TheUsername;Password=ThePassword;" providerName="System.Data.SqlClient" />

  • It does not appear to become a bug during my code/logic, as when the query has unsuccessful effectively, the code will begin correctly until next time the applying is reconstructed. It's clearly might be, and likely I'd need to apply a piece around during my code regardless of what anyway. :)

How to proceed?

Ideally, Let me "not consider the database schema" much. I'd want it to be because it appeared to stay in Scott Gu's excellent blog publish (and follow-up for dealing with existing databases) where things just exercised and disappeared away. Typically this is correct. It appears to become an problem using the connection not closed sooner or later, however i can't discover how you can rectify this problem.

A couple of forum / SO posts do imply the problem I am getting is essentially since the initializers aren't working just as planned and also the connection may be left open. The answer elsewhere seems to become to merely "don't make your own initializer", which is not the finest solution - but unless of course anybody has any ideas, I'll most likely need to do until CTP5 maybe.

*yep, I understand it is a CTP, so "supported" is most likely not the term :)