【发布时间】:2017-09-19 14:33:49
【问题描述】:
我正在对我的数据库进行多年以来第一次需要 EF 迁移的更改。这意味着在过去 2 年中发生了很多代码库更改,但这是我的模型多年来第一次更改。我正在使用安装在 LAN 上的服务器上的 EF 6.1.3、VS 2017 和 SQL Express。
我在项目上启用了 EF 迁移,并且能够“添加迁移”。然而,当谈到“更新数据库”时,我有以下错误:
在建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 错误定位服务器/指定实例)
我知道在未启用 TCP 连接时可能会发生这种事情,但我已经仔细检查过这一点,并且能够使用我的应用程序中的相同 DbContext 对象和连接字符串毫无问题地连接到数据库.我还可以使用 SQL Server 对象资源管理器。只有更新数据库失败。我在一个域上并且正在使用域级身份验证。
是否需要启用其他一些权限才能允许 EF 迁移工作?
编辑:对于那些建议在下面的 cmets 中检查启动项目的人,我提供了错误跟踪的开始。我没有提供完整的跟踪,因为它会带我们浏览整个函数名称的迁移堆栈。包括我的确切命令行在内的相关部分是:
PM> Update-Database -ProjectName "AJSoft.CN2.Data.Model" -Verbose
Using StartUp project 'AJSoft.CN2.Data.Model'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'CN2 Test' (DataSource: SERVER\SQLEXPRESS, Provider: System.Data.SqlClient, Origin: UserCode).
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)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling)
启动项目是可以访问连接字符串的项目,无论我指定上面的“模型”项目还是我实际的 WPF 启动项目,我的结果都是一样的。
【问题讨论】:
-
可以通过Sql Server Management Studio连接服务器吗?
-
是的 - 我的开发 PC 或本地 PC 都没有问题。
-
您是否在 conn 字符串中包含 \sqlexpress 后缀?
-
是的 - 我已经仔细检查过,无论我的代码还是 EF 迁移正在访问数据库,连接字符串都完全相同。
-
仅供参考,我还收到消息“目标数据库是:'CN2 Test'(数据源:SERVER\SQLEXPRESS,提供者:System.Data.SqlClient,来源:UserCode)。”这似乎是从我的 DbContext 中正确暗示的。
标签: c# sql-server visual-studio entity-framework-6 entity-framework-migrations