Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 938
SB2055 Avatar asked Jul 26 '13 16:07

SB2055


1 Answers

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.

like image 96
fourpastmidnight Avatar answered Sep 29 '22 03:09

fourpastmidnight