I am running integration tests. Before starting the test, I deleted the database and create it again. The first test is successful. But at the start of the second test I get an exception:
SetUp : System.Data.SqlClient.SqlException : Cannot drop database "Test" because it is currently in use.
Сode:
[TestFixture]
class Class1
{
public SqlConnection Repository;
[SetUp]
public void LocInit()
{
Repository = new SqlConnection(@"Data Source=.\SQLEXPRESS; Integrated Security=true;");
Repository.Open();
Repository.Execute("USE master;");
Repository.Execute("DROP DATABASE Test;");
Repository.Execute("USE master; CREATE DATABASE Test;");
Repository.Execute("USE Test;");
}
[Test]
public void Test1()
{
using (var repository = new SqlConnection(@"Data Source=.\SQLEXPRESS; Integrated Security=true;"))
repository.Execute("USE Test; SELECT 10");
}
[Test]
public void Test2()
{
using (var repository = new SqlConnection(@"Data Source=.\SQLEXPRESS; Integrated Security=true;"))
repository.Execute("USE Test; SELECT 10");
}
[TearDown]
public void LocalTearDown()
{
Repository.Dispose();
}
}
Why do I get this exception?