1

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?

Community
  • 1
  • 1
feradz
  • 1,312
  • 1
  • 16
  • 29
  • 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 Sep 19 '16 at 20:30
  • No this is not a duplicate and is not the same problem. I can connect to the server from other clients for example VS2015 SQL Server Object Explorer or Serer Management Studio. However my ASP.NET Core MVC Web Application fails. – feradz Sep 19 '16 at 20:33
  • Have you tried reducing your connection string to the bare minimum? Try it using the `Data Source`, `Initial Catalog`, `User ID` and `Password` parameters, leave all the others out. – Brad Sep 19 '16 at 23:47
  • Nope, reducing the connection string didn't help neither. – feradz Sep 20 '16 at 07:01
  • Can you try with following connection string? `Server=servername;Database=dbname;user=username;password=password;Trusted_Connection=False;` – VirendraJ Sep 20 '16 at 11:17

3 Answers3

2

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;"
RichL
  • 183
  • 1
  • 10
0

This problem happens because it seems that ASP.NET Core 1 does seems to have bug or does not support connection strings like that at least to SQL Server.

Specifically, the problem happens if you have a project which targets ASP.NET Core. For example, the problem will exists if you create ASP.NET Core Web Application (.NET Core) project but will not exists if you create ASP.NET Core Web Application (.NET Framework).

enter image description here

One way to work around this is to target the .NET Framework platform "net461": {} by changing your project.json file like that.

  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.0.1",
          "type": "platform"
        }
      },
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    },
    "net461": {}
  },

EDIT

This is a link to the git hub about the bug/issue described here. https://github.com/dotnet/corefx/issues/4586

feradz
  • 1,312
  • 1
  • 16
  • 29
  • Have you got a link to the relevant issue on Github? – Mike Brind Sep 21 '16 at 07:13
  • Hi Mike, I have just updated my answer by adding the link. https://github.com/dotnet/corefx/issues/4586 – feradz Sep 21 '16 at 14:11
  • That issue is not a bug and doesn't seem to apply to your situation. You don't appear to be using named instances form the information you supplied in your post. – Mike Brind Sep 22 '16 at 08:48
  • I might be confused that my problem is related to the mentioned asp.net core issue https://github.com/dotnet/corefx/issues/4586 but I assure you it does not work. You can try it yourself. Create one default WebApp, in your connection string use MS SQL server. – feradz Sep 22 '16 at 10:49
0

Today I faced the same issue.I tried many websites, at last, I found that we need to specify the server port number in the connection string.

 Server = tcp:<ServerName>,<PortName>; Database = <DataBaseName>; User Id = <UserName>; Password = <Password>;

To get the server port number using the following query.

select distinct local_net_address, local_tcp_port from sys.dm_exec_connections where local_net_address is not null
Sathish
  • 2,029
  • 15
  • 13