Cannot drop database because it is currently in use - EF code-first

11,144

If you were recently debugging your web application, ensure that the IIS Express isn't still running and that there are no w3wp.exe processes associated with IIS Express. This process may still be holding on to a database connection.

Share:
11,144
SB2055
Author by

SB2055

Updated on June 04, 2022

Comments

  • SB2055
    SB2055 almost 2 years

    In my configuration I have this:

    public sealed class Configuration : DbMigrationsConfiguration<App.Repository.NogginatorDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }
    
        protected override void Seed(AppDbContext context)
        {
            SqlConnection.ClearAllPools();
            //context.Database.CreateIfNotExists();
            System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseAlways<AppDbContext>());
    
            if (!WebMatrix.WebData.WebSecurity.Initialized)
            {
                WebSecurity.InitializeDatabaseConnection("TestConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
            }
         }
     }
    

    This is used for a test db that should drop and recreate every time. Though when I hit "update-database" from the package manager console, even if the database is deleted manually prior to running, I get:

    Cannot drop database "Nogginator.Test" because it is currently in use.

    My connection string:

    <add name="TestConnection" 
         providerName="System.Data.SqlClient" 
         connectionString="Data Source=.\;Initial Catalog=App.Test;Trusted_Connection=True;MultipleActiveResultSets=True;" />
    

    Why would this be happening?