Quick note about how to rename an SQL Server database that could not be exclusively locked (because it has open connections to it):
ALTER DATABASE currentDbName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
EXEC sp_renamedb N'currentDbName', N'newDbName';
GO
ALTER DATABASE newDbName SET MULTI_USER WITH ROLLBACK IMMEDIATE;
GO
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:
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.
Enter the command sqllocaldb infoat the prompt. The result is the name of your SQL Server LocalDB instance.
Enter the command sqllocaldb stop InstanceName. Replace “InstanceName” with the name you got from the previous command.
Enter the command sqllocaldb delete InstanceName. Replace “InstanceName” as in the command before.
Commands to stop and delete the LocalDB database.
Open the folder where the database files (*.mdf) are stored.
Open App-data folder in Windows Explorer
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.