【发布时间】: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