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.
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.
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.
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.
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.
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
Try to modify the number code of 'xxxxx-20170604092712' in Web.config file,
and then 'update-database' again. This worked for me.
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
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:
Related
I am attempting to create a new database in an existing sql server from code first. I used package manager to enable migrations, add a migration, then update database. The three methods executed without any errors and I got the traditional massages afterwards. However, when I look in either MSSSMS or Server Explorer I don’t see the database. I tried re-running the update database command and got the migration has already been applied. Any suggestions?
Update:
I've figured out that the project is adding the database to localdb. However, I have a connection string in the app.config file.
Probable causes are:
the database was never created
you use a login that can't see the database. Using Windows authentication/sa might allow you to see it and create user mappings for other logins.
you have multiple instances of sql running and you're connecting to the default instance while the database what created on a named instance
you accidentally created the database on another server
you connect to master by default with your software and instead of creating a database the database structure was created in the system database master
I've been working with MS sql for more than a decade and have struggled with issues like these, but in the end I could always explain what happened; if SQL returns it executed a query succesfully id did.
I accidentally dropped SQL Server database connection in SQL Server Management Studio. After i dropped the Connection and pushed "OK" the database was put into single user mode and then the database was taken offline and not shown in the object-explorer.
Is there any Point to rettach the database? I dont find any .BAK Datas or the Databasename in the Restore Options.
I tryed already, but get errors
USE master;
GO
ALTER DATABASE MyDB SET MULTI_USER
GO
I hope there is something i can do. Thx
EDIT:
Steps i did right Click on the Database -> Task -> Detach , than i was in the following Windows and clicked on "Drop Conections" and clicked "OK" and than she went into single user mode and went offline. But after a refresh i didnt saw her in my object Explorer. I followed this steps:
Getting exclusive access to restore SQL Server databases
so any way to get into single user mode to get her back?
See if any *.mdf files exist in file system for the dropped database. If you find, restore.
I have created a database in SQL Server 2012 with mdf and ldf pointing to a external hard drive attached to my machine. I created tables, stored procedures, populated tables, etc. etc.
I removed the hard drive at the end of the day.
Today, when I attached the hard drive and tried to access the DB in Management Studio, I see the name of the database with (Recovery Pending).
What does this mean? I see the mdf and ldf files in the D drive.
What worked for me was to take the database offline*, then back online - no RESTORE DATABASE was necessary in this case, so far as I can tell.
In SQL Server Management Studio:
right-click on the database
select Tasks / Take Offline ... breathe deeply, cross fingers...
right-click on the database again
select Tasks / Take Online
When you removed the drive, you forcefully disconnected the database from the SQL Server service. SQL Server does not like that.
SQL Server is designed by default so that any database created is automatically kept open until either the computer shuts down, or the SQL Server service is stopped. Prior to removing the drive, you should have "Detached" the database, or stopped the SQL Server service.
You "may" be able to get the database running by executing the following command in a query window: RESTORE DATABASE [xxx] WITH RECOVERY;
You could, although I would not normally recommend this, alter the database to automatically close after there are no active connections.
To accomplish this, you would execute the following query:
ALTER DATABASE [xxx] SET AUTO_CLOSE ON WITH NO_WAIT;
Another way that works is to "Restart" the Database Engine. If feasible and/or practical for this server, it may be faster whenever you have several DB in the external drive.
In SQL Server Management Studio:
Attach the external drive
right-click on the database engine : Server Name(SQL Server
12.0.2000 ... etc)
Select "Restart"
Answer Yes when asked if you want to proceed
Below worked for me:
Run SQL Management Studio as Administrator (right click on SQL
Management Studio icon and select 'Run As')
Take database offline
Detach the database using DROP option
Attach the database
If you were using this database with a Web App running on IIS then you may need to restart the IIS Server
Hope this helps someone
If the SQL Server knows that database recovery needs to be run but something is preventing it from starting, the Server marks the db in ‘Recovery Pending’ state. This is different from the SUSPECT state because it cannot be said that recovery is going to fail – it just hasn’t started yet.
Check this thread: How to fix Recovery Pending State in SQL Server Database?
I am in the process of cleaning up a problem I was having with SQL Server 2012 Express. I got locked out of Management Studio, and after many hours of researching, and trying different solutions with no success, I uninstalled SQL Server Management Studio and then re-installed SQL Server 2012 Express again.
I am able to get back into Management Studio. When I go to create my database again, I am given the error message the database exists, and it cannot be created. My database does not show in the Management Studio list of databases.
What do I need to do, so I can create my database again? There is still old data there from the previous install.
Look in the directories for the previous installation. I'd wager the .mdf and .ldf database files are still there and it's preventing you from creating the database. Alternatively you can try to attach the previous files, however if they were created from a higher level of SQL (such as standard or enterprise) they may not be compatible.
USE master;
GO
DROP DATABASE Database_Name;
GO
OR you can check for Existance of a Database 1st and if Exists then drop
USE master
GO
IF EXISTS(SELECT * FROM sys.databases
WHERE name = 'Database_Name')
DROP DATABASE Database_Name;
GO
Instead of Creating the same database, try importing the database. Have you looked at
Import Export SQL Server DB's
I was trying to detach the DB when it gave me an error that it is currently in use. Hence, I tried to take my DB offline but it failed saying
'an exception occured while executing a transact SQL statement or batch
-> ALTER DATABASE failed because a lock could not be placed on database 'myDB'. Try again later.
ALTER DATABASE statement failed. (Microsoft SQL Server, Error: 5061)'
Now if I try and access the DB it says it is not accessible. What can I do to make my DB accessible again?
My aim was to detach the DB, relocate its secondary database file to a new drive and reattach it (simply because of space issues).
Try following steps.
Restart the SQL server service using services.msc console.
Now connect to your server using SQL Server Management Studio.
Run following command in query analyzer
ALTER DATABASE `YOURDATABASE_NAME`
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Now Right-click the database name, point to Tasks, and then click Detach. The Detach Database dialog box appears
OR
5. Run your command to Relocate the secondary database.
Set the database mode to multi user again
ALTER DATABASE `YOURDATABASE_NAME` SET MULTI_USER
Hope this helps.
As an alternative to step one in Furqan's answer, you might not need to restart the SQL Server instance, but only the SQL Server Management Studio instance, which was used to initiate the "Take Offline" task.