How to reset a SQL Server LocalDB instance in Visual Studio

Many Visual Studio project-templates configure a SQL Server LocalDB instance for development on your local machine. For example the ASP.NET with Identity template.

But what to do if that database gets corrupted or you need a clean one for testing your Entity Framework Migrations, for example?

One solution is this:

  1. Open up the Package Manager Console (Tools -> NuGet Package Manager -> Package Manager Console). Make sure to select the project containing your database in the DefaultProject dropdown.
  2. Enter the command sqllocaldb infoat the prompt. The result is the name of your SQL Server LocalDB instance.
  3. Enter the command sqllocaldb stop InstanceName. Replace “InstanceName” with the name you got from the previous command.
  4. Enter the command sqllocaldb delete InstanceName. Replace “InstanceName” as in the command before.

    Commands to stop and delete the LocalDB database.
  5. Open the folder where the database files (*.mdf) are stored.

    Open App-data folder in Windows Explorer
  6. Delete the two *.mdf files in the folder.

Depending on your configuration your database will be re-created automatically when you execute your application or running Update-Databasein the Package Manager Console.

 

2 thoughts on “How to reset a SQL Server LocalDB instance in Visual Studio”

  1. Great… this just destroyed my solution… Local DB was NOT rebuilt after deleting it! How should it?

  2. Hi Alex, sorry for hearing that it did not work out for you. Have you tried running “Update-Database” or something else to have Visual Studio re-create your database? Please comment with a valid email-address next time so you can get any answers on your comment. Just venting your frustration it not very helpful.

Comments are closed.