C# 连接字符串问题:System.Data.SqlClient.SqlException(0x80131904)-无法连接到SQL Server Management Studio

C# 连接字符串问题:System.Data.SqlClient.SqlException(0x80131904)-无法连接到SQL Server Management Studio,c#,entity-framework-6,C#,Entity Framework 6,我正在尝试使用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.microsof

我正在尝试使用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>
然后启用迁移并创建第一次迁移:

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

如果有人有答案,请帮忙

非常感谢! 进一步的技术细节 目标框架:.NET Framework 4.5 操作系统:
IDE:(例如Visual Studio 2017 15.4)

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

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

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

仅供参考,键入
BolgDbContext
是故意的,从您的代码中复制粘贴

我找到了如下解决方案:

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


您连接的不是SQL management studio,而是SQL server实例。您能够从SQL management studio连接到SQL server实例吗?我找到了解决方案:
public class BolgDbContext : DbContext
{
    public BolgDbContext() : base("name=BlogDbContext") { }
    public DbSet<Post> Posts { get; set; }
}
<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>