【问题标题】:Connection String Issue: System.Data.SqlClient.SqlException (0x80131904) - Unable to connect to SQL Server Management Studio连接字符串问题:System.Data.SqlClient.SqlException (0x80131904) - 无法连接到 SQL Server Management Studio
【发布时间】:2018-05-30 07:11:45
【问题描述】:

我正在尝试使用我的 app.config 中的以下代码将 Visual Studio 2017 连接到 SQL Server 2016 Express:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
  </entityFramework>

  <connectionStrings>
<add name="BlogDbContext" connectionString="Data Source=DESKTOP-I3K90LQ\SQL2016;Initial Catalog=CodeFirstDemo;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
 </configuration>

我创建了一个示例存储库以在我的 program.cs 文件中进行测试

using System;
using System.Collections.Generic;
using System.Data.Entity;
sing System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
public class Post
{
    public int Id { get; set; }
    public DateTime DatePublished { get; set; }
    public DateTime DateEdited { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
}

public class BolgDbContext : DbContext
{
    public DbSet<Post> Posts { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
    }
}
}

我在包管理器控制台中使用以下命令将实体框架安装到我的控制台应用程序中

PM>install-package EntityFramework -version 6.1.3

然后我启用迁移并创建第一个迁移:

PM>enable-migrations
PM>add-migrations CreatePost
PM>update-database

提交更新数据库命令后,我收到以下错误消息:

System.Data.SqlClient.SqlException (0x80131904): 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

ClientConnectionId:00000000-0000-0000-0000-000000000000
Error Number:-1,State:0,Class:20
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

所以我的 Visual Studio 无法连接到我的 SQL Server Management Studio 来创建数据库。

如果这项工作正常,我的 Code First 控制台项目应该能够使用 Sql 实例而不是 LocalDb 实例从 Visual Studio 创建和更新数据库。 ### 昨晚我在谷歌上搜索了几个小时的解决方案。有人建议可能是防火墙问题,我应该创建一个端口 1433 并在 Sql Server 配置管理器中启用 TCP/IP 以允许 VS 连接到 SQL。我有所有这些解决方案,但没有一个适用于我的测试项目。

如果有人有答案,请帮忙。

非常感谢!

更多技术细节

目标框架:.NET Framework 4.5 操作系统: IDE:(例如 Visual Studio 2017 15.4)

【问题讨论】:

  • 您连接的不是 SQL 管理工作室,而是 SQL 服务器实例。您可以从 Sql management studio 连接到 SQL Server 实例吗?

标签: c# entity-framework-6


【解决方案1】:

可以肯定的是,默认情况下 EF 会在您的 web.config 中查找 DefaultConnection 连接名称。如果我错了,有人可以纠正我。

您需要在 DbContext 类上通过 :base() 覆盖连接名称。

public class BolgDbContext : DbContext
{
    public BolgDbContext() : base("name=BlogDbContext") { }
    public DbSet<Post> Posts { get; set; }
}

仅供参考,错字BolgDbContext 是故意的,从您的代码中复制粘贴...

【讨论】:

  • 我找到了解决方案:
【解决方案2】:

我找到了如下解决方案:

<connectionStrings>
<add name="BlogDbContext" connectionString="Data Source=DESKTOP-I3K90LQ\SQL2016;Initial Catalog=CodeFirstDemo;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=localhost; Integrated Security=True; MultipleActiveResultSets=True"/>
</parameters>
</defaultConnectionFactory>

<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>

【讨论】:

  • 谢谢!在 VS2019 中也是如此,使用 EntityFramework 和 Update-Database。相同的错误消息和解决方案,如下面我的回答中所述,更详细地说明了如何修复它。
【解决方案3】:

很高兴我找到了这个和你的解决方案!我只是在这里发帖描述我是如何遇到同样问题的,以及如何解决它的更多细节。

在 VS2019 中也是如此,使用 EntityFramework 和 Update-Database。同样的错误信息,即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).

我正在学习一个非常好的教程。它有 connectionString="Data Source=.;Initial Catalog=FriendOrganizer;Integrated Security=True". 这已经工作了一半的教程,并且 Update-Database 已经创建并播种了数据库,所以我知道数据库是可访问的。

在教程中应用了一些更改后,它停止了工作。我删除了 DB 和 Migrations,重新创建了 Migrations How to delete and recreate from scratch an existing EF Code First database,但它仍然无法正常工作,因为 Update-Database 在超时后产生了这个错误。

使用标志“-Verbose”在一定程度上有助于追踪它,但我需要这个答案!我去了Sql Server Object Explorer,右键单击第一个'(localdb)\MSSQLLocalDB...',得到Connection string 属性。我复制了这个并将其替换为数据访问项目的App.config 中的connectionStrings。然后更新数据库工作! (当我运行应用程序时,连接超时,因为我需要在启动项目App.config 中执行相同操作,但这可能不适用于所有情况,具体取决于您是否有两个App.configs)。

对于我的数据库,FriendOrganizerDb 连接字符串是...

    <connectionStrings>
    <add name="FriendOrganizerDb"
         connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=FriendOrganizer;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>

FriendOrganizer 是我正在关注的教程的数据库 - 强烈推荐! (Building an Enterprise App with WPF, MVVM, and Entity Framework Code First)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多