"Cannot Drop Database Because It Is Currently In Use". How To Fix?


Answer :

The problem is that your application probably still holds some connection to the database (or another application holds connection as well). Database cannot be deleted where there is any other opened connection. The first problem can be probably solved by turning connection pooling off (add Pooling=false to your connection string) or clear the pool before you delete the database (by calling SqlConnection.ClearAllPools()).

Both problems can be solved by forcing database to delete but for that you need custom database initializer where you switch the database to single user mode and after that delete it. Here is some example how to achieve that.


I was going crazy with this! I have an open database connection inside SQL Server Management Studio (SSMS) and a table query open to see the result of some unit tests. When re-running the tests inside Visual Studio I want it to drop the database always EVEN IF the connection is open in SSMS.

Here's the definitive way to get rid of Cannot drop database because it is currently in use:

Entity Framework Database Initialization

The trick is to override InitializeDatabase method inside the custom Initializer.

Copied relevant part here for the sake of good DUPLICATION... :)

If the database already exist, you may stumble into the case of having an error. The exception “Cannot drop database because it is currently in use” can raise. This problem occurs when an active connection remains connected to the database that it is in the process of being deleted. A trick is to override the InitializeDatabase method and to alter the database. This tell the database to close all connection and if a transaction is open to rollback this one.

public class CustomInitializer<T> : DropCreateDatabaseAlways<YourContext> {     public override void InitializeDatabase(YourContext context)     {         context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction             , string.Format("ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", context.Database.Connection.Database));          base.InitializeDatabase(context);     }      protected override void Seed(YourContext context)     {         // Seed code goes here...          base.Seed(context);     } } 

This is a really aggressive database (re)initializer for EF code-first with migrations; use it at your peril but it seems to run pretty repeatably for me. It will;

  1. Forcibly disconnect any other clients from the DB
  2. Delete the DB.
  3. Rebuild the DB with migrations and runs the Seed method
  4. Take ages! (watch the timeout limit for your test framework; a default 60 second timeout might not be enough)

Here's the class;

public class DropCreateAndMigrateDatabaseInitializer<TContext, TMigrationsConfiguration>: IDatabaseInitializer<TContext>      where TContext: DbContext     where TMigrationsConfiguration : System.Data.Entity.Migrations.DbMigrationsConfiguration<TContext>, new() {     public void InitializeDatabase(TContext context)     {         if (context.Database.Exists())         {             // set the database to SINGLE_USER so it can be dropped             context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, "ALTER DATABASE [" + context.Database.Connection.Database + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE");              // drop the database             context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, "USE master DROP DATABASE [" + context.Database.Connection.Database + "]");         }          var migrator = new MigrateDatabaseToLatestVersion<TContext, TMigrationsConfiguration>();         migrator.InitializeDatabase(context);      } } 

Use it like this;

public static void ResetDb() {     // rebuild the database     Console.WriteLine("Rebuilding the test database");     var initializer = new DropCreateAndMigrateDatabaseInitializer<MyContext, MyEfProject.Migrations.Configuration>();     Database.SetInitializer<MyContext>initializer);      using (var ctx = new MyContext())     {         ctx.Database.Initialize(force: true);     } } 

I also use Ladislav Mrnka's 'Pooling=false' trick, but I'm not sure if it's required or just a belt-and-braces measure. It'll certainly contribute to slowing down the test more.


Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?