I need to programmatically drop an EF core database. No problem, right? Get a context, call ctx.Database.EnsureDeleted(), and you're all set.
Except.. when the DB is in use, you're using connection pooling (there's good reason to do so), you can run into a situation where you can't drop the database.
Back in the EF6 days, I had a custom DB initializer which override InitializeDatabase as follows (setting the DB to single user mode so no other connections could interfere)
public override void InitializeDatabase(AudmDatabaseContext context)
{
context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction
, string.Format("ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", context.Database.Connection.Database));
base.InitializeDatabase(context);
}
Now I'm wondering.. how do you do this (or something else that gets me the same effect) using EF core 2.1?