61

Setup: Entity framework code first to new database.

Scenario: I'm playing around with EF and I add a bunch of elements to my database. I then change the entity model, and while I know that I could do migrations, I just want to start from scratch and basically wipe the database from the earth.

The database used by default was (localdb)\v11.0.

My question is:

Can I go somewhere and just delete a file, or start some kind of manager to delete that database and start from scratch?

Gabriel G. Roy
  • 2,552
  • 2
  • 27
  • 39

11 Answers11

70

Just go in command prompt with admin rights and type:

//list the instancies
sqllocaldb i

//stop selected instance
sqllocaldb p "selected instance"

//delete
sqllocaldb d "selected instance"

//recreate or create new one 
sqllocaldb c "new instance"
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Filip Gjorgjevikj
  • 1,367
  • 13
  • 11
  • 4
    Deleting the instance does not delete the actual database file. When EF recreates it the existing MDF file is (sometimes) reused. – MEMark Sep 17 '14 at 16:15
  • 1
    Awesome, thanks. That should be the accepted answer. Regarding the previous comment: That's a program call 'file explorer' becomes handy ;-) – Anytoe Nov 25 '15 at 18:57
26

From Visual Studio => Click View => SQL Server Object Explorer=> Right click the desired database and choose delete and it will be deleted or do whatever you want

18

If you're using Entity Framework Core, you can enter this in the Package Manager Console:

PM> Drop-Database

It will drop the current database. This command will tell you which one:

PM> Get-DbContext

This is also handy:

PM> Get-Help about_EntityFrameworkCore

Instead of the Package Manager Console, you can also use the dotnet CLI via PowerShell or the command prompt:

PS> dotnet ef database drop

Make sure you install the EF extension first by running this command:

dotnet tool install --global dotnet-ef
officer
  • 2,080
  • 1
  • 20
  • 29
Matt Gregory
  • 8,074
  • 8
  • 33
  • 40
7

I think you want to delete an individual database, not a LocalDB instance. If so, just issue a drop database command:

DROP DATABASE databasename;

You can do this from sqlcmd, Management Studio, your application code, maybe even Visual Studio...

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
5

There's an exe called SqlLocalDB.exe which can be found in C:\Program Files\Microsoft SQL Server\{version}\Tools\Binn

For a full delete:

>sqllocaldb stop InstanceName
>sqllocaldb delete InstanceName

Once that is done, you can optionally also remove the .mdf files from the disk. Mine were at

C:\Users\{username}

You can also recreate a fresh instance, and even attach the .mdf files you still want afterwards.

>sqllocaldb create InstanceName
>sqllocaldb start InstanceName
>sqllocaldb info InstanceName

You can see your instances at:

C:\Users\{username}\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances
CRice
  • 12,279
  • 7
  • 57
  • 84
4

Yes you can. In VS 2015/2017 press Ctrl+Q, type "object explorer". The "SQL Server Object Explorer" should open, where you'll see your local DB instances. Expand the DB instance, and you'll see the different databases. Select one database perform a right click and choose "Delete".

For additional information check this link.

Hope that helps.

Moerwald
  • 10,448
  • 9
  • 43
  • 83
2

With Entity Framework Core, this can also be accomplished form the the command line:

dotnet ef database drop --project [path to project]

Or if you have multiple database contexts:

dotnet ef database drop --project [path to project] --context [ContextName]

Make sure you install the EF extension first by running this command:

dotnet tool install --global dotnet-ef
officer
  • 2,080
  • 1
  • 20
  • 29
Mike Godin
  • 3,727
  • 3
  • 27
  • 29
1

LocalDB is its own separate server (its name suggests that it is just a database in some other server instance but this is not the case). In SQL Server 2014 Express you connect to it using server name "(localdb)\MSSQLLocalDB", just as you would connect to any ordinary database server. If you connect using SQL Server Management Studio then you have all the power of SSMS available to you.

0

Entity Framework 3.1:

Drop Database

dotnet ef database drop

Do not want to drop database, willing to delete tables in the database

dotnet ef database update 0
Nick B
  • 249
  • 1
  • 4
  • 11
0

I wrote a batch file to delete an SQL local db. It uses trusted connection. If you want, you can add your credentials with -U Username -P Password parameters.

Check out sqlcmd documentation!

enter image description here

@echo off
set /p DbName=Enter local db name:
echo.
sqlcmd -e -S "(LocalDb)\MSSQLLocalDB" -d "master" -Q "EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'%DbName%'; USE [master]; DROP DATABASE [%DbName%];"
echo Finished!
pause > nul
Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55
0

if you are having the following error:

FormatMessageW failed. Error code returned: 15100 

or

FormatMessageW failed. Error code returned: 15105

check to make sure you are using the right version of sqllocaldb.exe

You can verify the version using:

where.exe sqllocaldb
C:\Program Files\Microsoft SQL Server\150\Tools\Binn\SqlLocalDB.exe
C:\Program Files\Microsoft SQL Server\130\Tools\Binn\SqlLocalDB.exe

depending on the order you install sql server, the newer version could be later in the path and it will report the error above.

Justin
  • 1,303
  • 15
  • 30