Sql-server – Cannot connect to SQL Server on ASP.NET Core 1 EF7

asp.net-coreasp.net-core-1.0entity-framework-coresql server

I am trying to connect to SQL Server 2014 or 2016 from an ASP.NET Core 1.0 web application (Entity Framework Core). I am getting the error below. Any tips how to resolve it.

I can successfully connect to the DB from through Visual Studio 2015 SQL Server Object Explorer. However the connection from the web application fails. Neither I can do dotnet ef database update.

I can connect to LOCALDB.

My connection string looks like:

"DefaultConnection": "Data Source=server;Initial Catalog=TestDb;Integrated Security=False;User ID=sa;Password=*****;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"

The error I get is. I get the same error on SQL Server 2014 and 2016:

An unhandled exception occurred while processing the request.

Win32Exception: The network path was not found
.ctor

SqlException: 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)

The stack trace is:

Win32Exception: The network path was not found
.ctor
CreateConnection
CreatePooledConnection
CreateObject
UserCreateRequest
TryGetConnection
WaitForPendingOpen
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
GetResult
MoveNext in AccountController.cs
                var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext

EDIT 1

Please note, this is not the same question as: Why am I getting "Cannot Connect to Server – A network-related or instance-specific error"?

EDIT 2

The type of this project where I have problem is ASP.NET Core Web Application (.NET Core).

However I have tried by creating ASP.NET Core Web Application (.NET Framework) project and there was no problem there. I was able to connect.
ASP.NET Core Web Application (.NET Core) does not connect to the SQL server and .NET Framework) projects connects

Any ideas what is different between these projects?

Best Answer

I had a similar problem today. It seems that the .Net Core connection string to SQL Server is a little different from prior .Net SQL Server connection strings. Have you tried using something similar to the one specified here on the MSDN Blog?:

using (var connection = new SqlConnection("Server=tcp:YourServer,1433;Initial Catalog=YourDatabase;Persist Security Info=True;"))

(If you scroll down the page to the 'Relational databases' and 'SQL Server' sub heading, you will find examples.)

If you are using username and password in the string, you could also try this:

"Server=tcp:yourservername,1433;Initial Catalog=yourdbname;User ID=yourusername;Password=yourpassword;"