title: Cannot drop database because it is in use + EF Code first
body: <p>I am new to asp .net mvc3</p>
  
  <p>I am trying to store additional user information over the standard asp .net membership provider. I have created a new entity UserAddress and I sync it with the aspnet_tables using the membership API.</p>
  
  <p>However, now if I make any changes to the UserAddress class - asp.net does not drop and recreate the model. I get the error</p>
  
  <p>Cannot drop database because it is in use.</p>
  
  <p>I have searched stackoverflow and google. Basically, my problem is similar to this</p>
  
  <p><a href="https://stackoverflow.com/questions/5288996/database-in-use-error-with-entity-framework-4-code-first">Database in use error with Entity Framework 4 Code First</a></p>
  
  <p>However, I am not sure how to implement what chris suggest as a solution:</p>
  
  <p>"This was happening to me because I was calling Membership methods against the DB and that was creating a conflict. I resolved this by forcing the initializer to run and seed before using the Membership system"</p>
  
  <p>I am pretty sure this is the reason why the database is in use because my code falls over at this point in the data repository:</p>
  
  <pre><code>var userid = Membership.GetUser.ProviderUserKey.ToString();
  return db.UserAddress.SingleOrDefault(a =&gt; a.UserId == userid);
  </code></pre>
  
  <p>How can I force the initializer to run before membership system?</p>
  
  <p>Snippet of my code is below:</p>
  
  <pre><code>public class UserAddress
  {
      public int UserAddressId { get; set; }
      public string UserId { get; set; }
      public string FlatNo { get; set; }
      public string BuildingName { get; set; }
      public string Address1 { get; set; }
      public string Address2 { get; set; }
      public string PostCode { get; set; }
      public string City { get; set; }
      public string State { get; set; }
      public string Country { get; set; }
      public long MobilePhone { get; set; }
  }
  
  public class NPLHWebsiteDb : DbContext
  {
      public DbSet&lt;UserAddress&gt; UserAddress { get; set; }
  }
  
  public class NPLHWebsiteInitializer : DropCreateDatabaseIfModelChanges&lt;NPLHWebsiteDb&gt;
      {
          protected override void Seed(NPLHWebsiteDb context)
          {
              var userAddress = new List&lt;UserAddress&gt;
              {
                  new UserAddress {
                      UserAddressId = 1,
                      UserId = "153",
                      FlatNo = "6.4",
                      BuildingName = "Wilton Plaza",
                      MobilePhone = 9810110123,
                  }
              };
              userAddress.ForEach(a =&gt; context.UserAddress.Add(a));
              context.SaveChanges();
      }
  </code></pre>
  
  <p>Thanks</p>