Cannot connect to SQL Server on ASP.NET Core 1 EF7

て烟熏妆下的殇ゞ 提交于 2019-12-11 05:24:12

问题


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.

Any ideas what is different between these projects?


回答1:


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;"



回答2:


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).

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




回答3:


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


来源:https://stackoverflow.com/questions/39581316/cannot-connect-to-sql-server-on-asp-net-core-1-ef7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!