I've configured an .mdf
file along with a localdb connection string for use in unit tests, like this:
<connectionStrings>
<add name="TestData" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\v11.0; AttachDBFilename='|DataDirectory|\TestData.mdf'; Integrated Security=True"/>
</connectionStrings>
Once I've configured deployment files for my test correctly, this works beautifully: a copy of the .mdf is attached to the default instance of LocalDB, and the SqlClient
connects to it without a shred of configuration. It just works.
But how do I clean up afterwards? On my local box, I can periodically use SSMS to manually detach old testing databases, but on a CI server it would obviously be preferable to have the unit test clean itself up.
Is there a similarly automagic way to cause a localdb database to detach itself from the instance?
LocalDB is absolutely supported in production. From the performance point of view it is identical to SQL Server Express, as they share the same database engine.
Visual Studio SSDT will automatically create a test template however we need to change this unit test method. We will develop the test method according to following sample test document. We will select Pre-test option in the test condition combobox then add the following query.
This is how I am deleting the localDB database. The thing I don't like is that the .mdf is also removed. I overcome that by copying it to a tem directory first and using the copy to create the db.
var sc = new Microsoft.SqlServer.Management.Common.ServerConnection(your localDB SqlConnection here);
var server = new Microsoft.SqlServer.Management.Smo.Server(sc);
server.KillDatabase(dbName here);
Hope this helps Phil
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With