8

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.

John Edwards
  • 1,536
  • 3
  • 14
  • 33
  • Check this: http://stackoverflow.com/questions/9945409/how-do-i-fix-the-error-named-pipes-provider-error-40-could-not-open-a-connec and this: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/2a8f2e64-74cf-46f2-b2be-bbb08b1502cb/re-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server?forum=sqlreportingservices – Pawel Mar 15 '17 at 16:46
  • I have seen the general answers, but the problem is it WORKS perfectly in one project, but not the other. Am I missing something? – John Edwards Mar 15 '17 at 16:49
  • Silly tip... Can you debug and check which exact connection string is being used at runtime? – Chetan Mar 15 '17 at 17:19
  • I am running "dotnet ef database update" in the package manager console, so the program isn't even running, so i KNOW it is using the connection string in my appsettings.json file. – John Edwards Mar 15 '17 at 17:33
  • Possible duplicate of [Why am I getting "Cannot Connect to Server - A network-related or instance-specific error"?](http://stackoverflow.com/questions/18060667/why-am-i-getting-cannot-connect-to-server-a-network-related-or-instance-speci) – Igor Mar 16 '17 at 17:25

4 Answers4

10

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.

Bigabdoul
  • 721
  • 6
  • 12
5

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.

John Edwards
  • 1,536
  • 3
  • 14
  • 33
  • Oh my goodness! Had this EXACT same problem! This is a really sneaky thing for EF Core Migrations to do, because the connection string it auto-added is different from the one we use when deploying (of course). Banged my head for hours until I found your answer! THANK YOU. – starmandeluxe Aug 16 '17 at 07:57
2

My connectionString was copied and put hardcoded into my with Entity Framework scaffolded DbContext. That's why my app wasn't using the connectionString in appsettings.json.

Jaap Weijland
  • 3,146
  • 5
  • 23
  • 31
0

you should install MSSQLSERVER , EF Core cannot connect to SQLEXPRESS

msn.secret
  • 419
  • 5
  • 11