【问题标题】:Cannot connect to SQL Server on ASP.NET Core 1 EF7无法连接到 ASP.NET Core 1 EF7 上的 SQL Server
【发布时间】:2017-01-27 14:58:39
【问题描述】:

我正在尝试从 ASP.NET Core 1.0 Web 应用程序(Entity Framework Core)连接到 SQL Server 2014 或 2016。我收到以下错误。任何提示如何解决它。

我可以通过 Visual Studio 2015 SQL Server 对象资源管理器成功连接到数据库。但是,来自 Web 应用程序的连接失败。我也做不到dotnet ef database update

我可以连接到 LOCALDB。

我的连接字符串如下所示:

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

我得到的错误是。我在 SQL Server 2014 和 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)

堆栈跟踪是:

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

编辑 1

请注意,这不是同一个问题:为什么我收到"Cannot Connect to Server - A network-related or instance-specific error"?

编辑 2

我遇到问题的项目类型是 ASP.NET Core Web Application (.NET Core)

但是我已经尝试创建 ASP.NET Core Web Application (.NET Framework) 项目并且那里没有问题。我能够连接。

你知道这些项目有什么不同吗?

【问题讨论】:

  • 不,这不是重复的,也不是同一个问题。我可以从其他客户端连接到服务器,例如 VS2015 SQL Server Object Explorer 或 Serer Management Studio。但是我的 ASP.NET Core MVC Web 应用程序失败了。
  • 您是否尝试过将连接字符串减少到最低限度?尝试使用Data SourceInitial CatalogUser IDPassword 参数,将所有其他参数排除在外。
  • 不,减少连接字符串也没有帮助。
  • 你可以试试下面的连接字符串吗? Server=servername;Database=dbname;user=username;password=password;Trusted_Connection=False;

标签: sql-server asp.net-core entity-framework-core asp.net-core-1.0


【解决方案1】:

我今天遇到了类似的问题。到 SQL Server 的 .Net Core 连接字符串似乎与之前的 .Net SQL Server 连接字符串略有不同。您是否尝试过使用类似于 specified here on the MSDN Blog? 的东西:

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

(如果您向下滚动页面到“关系数据库”和“SQL Server”子标题,您会找到示例。)

如果你在字符串中使用用户名和密码,你也可以试试这个:

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

【讨论】:

    【解决方案2】:

    出现此问题是因为 ASP.NET Core 1 似乎确实存在错误或不支持至少对 SQL Server 而言这样的连接字符串。

    具体来说,如果您有一个以 ASP.NET Core 为目标的项目,就会出现问题。例如,如果您创建 ASP.NET Core Web Application (.NET Core) 项目会存在问题,但如果您创建 ASP.NET Core,则不会存在问题Web 应用程序(.NET 框架

    解决此问题的一种方法是通过更改您的 project.json 文件来定位 .NET Framework 平台 "net461": {}

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

    编辑

    这是关于此处描述的错误/问题的 git 中心的链接。 https://github.com/dotnet/corefx/issues/4586

    【讨论】:

    • 你有 Github 上相关问题的链接吗?
    • 嗨,迈克,我刚刚通过添加链接更新了我的答案。 github.com/dotnet/corefx/issues/4586
    • 该问题不是错误,似乎不适用于您的情况。根据您在帖子中提供的信息,您似乎没有使用命名实例。
    • 我可能会感到困惑,我的问题与提到的 asp.net 核心问题 github.com/dotnet/corefx/issues/4586 有关,但我向您保证它不起作用。你可以自己试试。创建一个默认 WebApp,在您的连接字符串中使用 MS SQL 服务器。
    【解决方案3】:

    今天遇到同样的问题,试了很多网站,最后发现需要在连接字符串中指定服务器端口号。

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

    使用以下查询获取服务器端口号。

    select distinct local_net_address, local_tcp_port from sys.dm_exec_connections where local_net_address is not null
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 2022-06-19
      相关资源
      最近更新 更多