Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio is trying to connect SQL database while loading a solution. Gives error: Cannot open database requested by the login

I've got a Visual Studio solution on which I had been playing with few C# projects related to Entity Framework coding.

I was creating some test databases to connect to and learn db first and code first approach. The problem is now every time I try to open the solution containing those projects, for some weird reason Visual Studio, or the solution or some project in it tries to connect to those databases. It gives an error as I've deleted those databases:

Cannot open database "Company" requested by the login. The login failed. Login failed for user "administrator".

Here is the error message snapshot:

enter image description here

I get this error one by one for several databases. But I'm wondering why would VS try to connect to some database just while loading the solution? In case anyone else has faced this situation or know the reason behind it then that will be really great.

Note: The tags in the question I've put is only due to the environment I was working into. I'm trying to reach to a point to figure out if my problem is specifically related to those areas.

like image 308
RBT Avatar asked Oct 04 '16 11:10

RBT


People also ask

Could not connect to the database Cannot open database?

In the security panel, click the option under Server authentication for SQL Server and Windows Authentication mode. Click OK to save the settings and close the Properties dialog box. Add a new SQL login by expanding the Security folder and clicking the Logins folder. Right-click the Logins folder and click New Login.

How do I connect Microsoft Visual Studio database?

Connect to your database In Visual Studio Code, press Ctrl+Shift+P (or F1) to open the Command Palette. Select MS SQL:Connect and choose Enter. Select Create Connection Profile.

Can not open database?

If you are receiving the error message "Cannot open database. It may not be a database that your application recognizes, or the file may be corrupt.", check if the database has reached or exceeded the 2 GB limit.


1 Answers

TL;DR; Delete the inactive/invalid SQL Server connections marked with a red cross in the Server explorer window of Visual Studio to get rid of the issue. SQL Server connections become invalid/stale whenever you delete the corresponding database from SQL Server but Visual Studio doesn't know about it and keeps trying unnecessarily.

Details: As correctly suggested by @stuartd, the Server Explorer in Visual Studio was messing up. Posting the answer in case it helps anyone else as well.

I realized that every time I was adding an ADO.NET Entity Data Model file for Entity Framework into my project as shown below it was adding a SQL connection node in Server Explorer window as shown below.

enter image description here

The connection was corresponding to the DB I chose in the ADO.NET entity data model addition wizard. I see that there is red cross against few of them since I've deleted the databases from SQL Server instance. Whenever you load a solution Visual Studio will always try to refresh the connection of all the databases currently found in Server Explorer. In fact in the case of DBs which have a red mark against them (as they are deleted) I'm sure VS must be employing the retry logic as well while trying to make a connection with the database. All this takes and wastes time as far as the loading time of a solution is concerned.

enter image description here

How to resolve the error: I deleted the inactive/unused connections marked with a red cross to get rid of the issue. It will be great if someone knows a way to mark the connections as inactive i.e. the connections live in the Server Explorer but VS shouldn't try connecting them every time until I ask it to refresh or may be it can refresh when I expand its node.

I also observed that Visual Studio somehow maintains these server explorer connections on a per solution file basis, may be in *.suo files. I had to delete those stale connections after opening every solution which were facing this error related to DB connection issue while getting loaded in Visual Studio. All solutions facing this issue somehow had at least one project related to entity framework :).

like image 137
RBT Avatar answered Sep 20 '22 15:09

RBT