Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core Cannot Connect to Server - A network-related or instance-specific error

Important : Before people point to the common causes, the connection string works in one project on my local machine, but not the other project on the same local machine connecting to the same local instance to the SAME database.

I am having a weird issue. I have 2 EF core projects. My connection string in the first project works perfectly. I can type "dotnet ef database update", and it will push my migration to the LOCAL database. I copy pasted the below string EXACTLY to my 2nd project, and it throws the error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Here is my connection string:

Server=.;Database=RAMSAPPDB;Integrated Security=False;Persist Security Info=True;MultipleActiveResultSets=True;

I cannot think of a single reason why using this connection string in my other project, which tries to add to the SAME database, would throw this error. I tried removing all the tables in the RAMSAPPDB before pushing the migrations in the 2nd project, but it just doesn't work.

If I comment out the "ConnectionString", I get the error

connectionString cannot be blank

so I know it is using the connection string in my appsettings.json.

Any ideas on possible causes would be greatly appreciated. Thank you.

like image 281
John Edwards Avatar asked Mar 15 '17 16:03

John Edwards


People also ask

Could not connect to server a network related or instance specific error occurred while establishing?

1) Open Sql Server Configuration Manager --> SQL Server Network configuration --> Protocols for <(INSTANCE)> --> TCP/IP (double click on it). 2) Select --> IP Addresses(Tab). 3) Go to the last entry IP All and mention TCP Port 1433. 4) Press Win+R and enter services.

Why is SQL Server not connecting?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.

How do I fix SQL Server error 53?

User Action. Make sure that you have entered the correct server name on the client, and that you can resolve the name of the server from the client. To check TCP/IP name resolution, you can use the ping command in the Windows operating system.


2 Answers

This is an issue sometimes caused by the appsettings.json file when in development mode. If that's the case then you should look for the appsettings.Development.json file and do the changes in there.

If you're in Visual Studio and related file nesting is enabled, you can access appsettings.Development.json by clicking the arrow next to appsettings.json, or open it and right-click on its title bar then choose "Open Containing Folder". appsettings.Development.json should be there, change the connection string to what you wish and you're good to go.

like image 172
Bigabdoul Avatar answered Oct 20 '22 00:10

Bigabdoul


I resolved it. I had written this a long time ago and forgot that

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

optionsBuilder.UseSqlServer("Server=.\\SQLEXPRESS;Database=MYDB;Integrated     Security=True;MultipleActiveResultSets=True;");

}

in my DbContext class was being used when I ran the "dotnet ef database update" command. I thought it was using what was in my appsettings.json.

like image 27
John Edwards Avatar answered Oct 20 '22 00:10

John Edwards