title: CodeFirst: Cannot open database "MyDatabase " requested by the login. The login failed. Login failed for user 'MyDomain\MyUser'
body: <p>So I've been getting this error all of a sudden, and I have no idea how to resolve it. <code>MyDomain\MyUser</code> is listed under server users, but since I'm using CodeFIrst, the database is not created yet, so I can't add <code>MyDatabase</code> to the user.</p>
  
  <p>Here is my code:</p>
  
  <p>ConnectionString:</p>
  
  <pre><code>&lt;add name="MyDatabaseConnection" connectionString="Data Source=myServer; Database=MyDatabase.sdf;Trusted_Connection=True;Persist Security Info=True;Pooling=false" providerName="System.Data.SqlClient" /&gt;
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>public class MyDbContext: DbContext
  {
      public MyDbContext()
          : base("MyDatabaseConnection")
      {
      }
  
      public DbSet&lt;User&gt; Users { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
          modelBuilder.Configurations.Add(new UserConfiguration());
      }
  }
  
  
  public class UserConfiguration : EntityTypeConfiguration&lt;User&gt;
  {
      public UserConfiguration()
      {
          ToTable("User");
          Property(u =&gt; u.ID).HasColumnName("UserID").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
      }
  }
  
  
  protected void Application_Start()
  {
      Database.SetInitializer&lt;MyDbContext&gt;(new MyDbContextInitializer());
      using (MyDbContext db = new MyDbContext())
      {
          db.Database.Initialize(false);
      }
      ...
  }
  
  public class MyDbContextInitializer : DropCreateDatabaseIfModelChanges&lt;MyDbContext&gt;
  {
      protected override void Seed(MyDbContextcontext)
      {
          base.Seed(context);
          SeedMembership();
      }
  
      private void SeedMembership()
      {
          WebSecurity.InitializeDatabaseConnection(ConfigurationManager.ConnectionStrings["MyDatabaseConnection"].ConnectionString, "System.Data.SqlClient", "User", "UserID", "UserName", true);
          WebSecurity.CreateUserAndAccount("doe1", "password11", new { FirstName = "John", LastName = "Doe" });
      }
  }
  </code></pre>