Stephen Paul
Stephen Paul

Reputation: 39015

Database '[DatabaseName]' already exists. Choose a different database name

I'm following this guide illustrating a code-first approach with Entity Framework core. Migrations were functioning correctly until, at some point, I deleted my .mdf file. Since then, executing Update-Database (to apply my migration) throws the following error: Database 'DatabaseName' already exists. Choose a different database name.

Where exactly is this database? How can I remove it permanently?

According to this answer, I need to detach my database from Sql Server, but I'm not sure how to do that now. In Sql Server Management studio, If I execute sp_detach_db DatabaseName it throws error The database 'DatabaseName' does not exist. Supply a valid database name.

Thanks in advance.

UPDATE I see I can also reproduce this database already exists error if I have the database attached in SQL Server Management Studio, and execute the Update-Database command. After I close the management studio, the migration applies without this error. Very confusing to me.

Upvotes: 20

Views: 32373

Answers (9)

cryss
cryss

Reputation: 4499

In my case, I wanted to run migrations straight after starting the SQL server on linux in docker container. The thing was that the SQL server wasn't yet fully started. So after 30s the migrations went just fine.

Upvotes: 2

Marta
Marta

Reputation: 326

I am using MSSQLLocalDB. I've encountered this issue after restoring the backup of a DB using the Azure Data Studio.

When I connected using Microsoft SQL Management Studio, I've noticed that my DB was in a SINGLE_USER mode (I have not noticed it in the Azure Data Studio).

Changing the mode to MULTIPLE_USER fixed the issue for me:

enter image description here

Upvotes: 0

Juan Felipe V V
Juan Felipe V V

Reputation: 1

I restore my database to another server, To fix this I had to remove from the DB under Security the previous users and create them again

Upvotes: 0

akd
akd

Reputation: 6758

Another guess.. if you have restored the database then your user login will not work anymore because of the orphan. Try to remove user and create it again. you will get the same error if your migration cannot access the database.

Upvotes: 3

chaosifier
chaosifier

Reputation: 2964

Seems like an instance of LocalDB is still running in the background. Execute the following commands in the console to stop and delete the instance.

sqllocaldb stop
sqllocaldb delete

The Update-Database command should now have no problems.

Upvotes: 37

sosay
sosay

Reputation: 11

If you create your database with SQL (meaning DB already exists), put the line DROP DATABASE [databaseName] at the beginning of the file databaseName.sql

This will delete the whole DB and its definition (schema), now you can start with creating your new DB.

Upvotes: 1

Mystic Lin
Mystic Lin

Reputation: 375

Try to modify the number code of 'xxxxx-20170604092712' in Web.config file,

and then 'update-database' again. This worked for me.

Upvotes: 0

Alexander Zinoviev
Alexander Zinoviev

Reputation: 144

Please look at the SQL Server Object Explorer (Visual Studio -> View -> SQL Server Object Explorer). If it contains the 'DatabaseName' database then please try to delete it.

Upvotes: 10

rborob
rborob

Reputation: 38

It might be a local DB. Try connecting to your local DB

(LocalDb)\MSSQLLocalDB

And seeing if the DB is located there. You may need to delete it from SSMS

Upvotes: 0

Related Questions