2

I am writing some UnitTests with NUnit and Entity Framework. How to delete the whole localdb database from Entity Framework level?

Note: I don't want to clear the tables' data. I want to delete the whole database.

Also I am able to create a localdb file in my application working directory provided the database had not been created:

string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "");
var testDbFileName = String.Format(@"UnitTestDB.mdf");
var testDbFileNameWithPath = path + @"\" + testDbFileName;

var connectionString =
String.Format(
    @"Data Source=(localdb)\V11.0;Initial Catalog={0};Integrated Security=True;AttachDBFilename={1};MultipleActiveResultSets=True",
    "UnitTestDB", testDbFileNameWithPath);

//Here would be the code passing connection string to DbContext and so on..

Deleting only the file "UnitTestDB.mdf" is not enough. There is still a reference to the db in SQL Management Studio

2 Answers 2

5

There are 2 ways to do this in code. Stick to EF code first if you can :-)

1) EF has a nice option on the context.

 Context.Database.Delete()

2) if you want old school SQLCommand/SqlConnection approach something like this hack routine...

 public  bool DropDB(string DBName, string ConnectionString)
    {

        SqlConnection conn = null;
        SqlCommand cmd = null;
        string stmt = null;

        int rowCount = 0;

        if (string.IsNullOrEmpty(DBName))
            throw new ArgumentNullException(DBName, "DBName required");

        try
        {
            conn = new SqlConnection(ConnectionString);

           stmt = "DROP DATABASE " + DBName ;

            cmd = new SqlCommand(stmt, conn);
            conn.Open();
            rowCount = cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch (Exception)
        {
            //todo  whatever
            throw;
        }
        finally
        {
            if (conn != null) conn.Dispose();
            if (cmd != null) conn.Dispose();
        }
        if (rowCount == -1) return true;
        return false;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, very helpful answer. Before I read it I had been creating a new db in a TestFixture by generating a new Guid so the db (and its structure) had been being recreated each time. Then I was deleting all the databases from SQL Management Studio level.
0

You can also use a shell command to delete it. If you want, you can add your credentials with -U Username -P Password parameters.

Check out sqlcmd documentation!

sqlcmd -e -S "(LocalDb)\MSSQLLocalDB" -d "master" -Q "EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'your-database-name'; USE [master]; DROP DATABASE [your-database-name];"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.