EF6 连接更新数据库命令时出现问题
EF6 Problems Connecting for Update-Database Command
多年来我第一次更改需要 EF 迁移的数据库。这意味着在过去 2 年中代码库发生了很多变化,但这是我的模型多年来第一次发生变化。我正在使用安装在局域网服务器上的 EF 6.1.3、VS 2017 和 SQL Express。
我在项目上启用了 EF 迁移并且能够 "Add-Migration"。然而,当谈到"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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
我知道在未启用 TCP 连接时可能会发生这种情况,但我仔细检查了这一点,并且能够使用相同的 DbContext 对象和连接字符串毫无问题地连接到数据库我的应用程序。我还可以使用 SQL 服务器对象资源管理器。只有 Update-Database 失败了。我在一个域上并且正在使用域级身份验证。
是否需要启用其他一些权限才能使 EF 迁移工作?
编辑:对于那些在下面的评论中提出检查启动项目等建议的人,我提供了错误跟踪的开始。我没有提供完整的跟踪,因为它会带我们浏览整个函数名称的迁移堆栈。包括我的确切命令行在内的相关部分是:
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)
启动项目是可以访问连接字符串的项目,无论我指定上面的"Model"项目还是我实际的WPF启动项目,我的结果都是一样的。
好的 - 对于其他人来说,我已经解决了这个问题。
进一步思考后,不得不 为我的项目和 EF 迁移使用不同的连接字符串,否则消息没有意义。
提供给 EF 迁移的连接字符串是在我的自定义 DbContext class 的静态构造函数中设置的。就我而言,这是正确的行为。 但是 当项目被编译并且 运行 时,设置文件被读取并且我的代码向连接字符串添加了一些其他信息。
这就是两者之间存在差异的原因 - 在静态构造函数中将信息添加到 CS 修复了错误。
我的问题是我为包管理器控制台设置的环境 (ASPNETCORE_ENVIRONMENT) 与我的 .NET Core 项目不同。
在程序包管理器控制台中使用 Get-ChildItem Env:
列出您的环境变量。
我用Update-Database -Verbose
查看命令使用的是哪个环境
然后我更改了环境变量,重新启动 Visual studio,它使用了正确的项目设置。
多年来我第一次更改需要 EF 迁移的数据库。这意味着在过去 2 年中代码库发生了很多变化,但这是我的模型多年来第一次发生变化。我正在使用安装在局域网服务器上的 EF 6.1.3、VS 2017 和 SQL Express。
我在项目上启用了 EF 迁移并且能够 "Add-Migration"。然而,当谈到"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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
我知道在未启用 TCP 连接时可能会发生这种情况,但我仔细检查了这一点,并且能够使用相同的 DbContext 对象和连接字符串毫无问题地连接到数据库我的应用程序。我还可以使用 SQL 服务器对象资源管理器。只有 Update-Database 失败了。我在一个域上并且正在使用域级身份验证。
是否需要启用其他一些权限才能使 EF 迁移工作?
编辑:对于那些在下面的评论中提出检查启动项目等建议的人,我提供了错误跟踪的开始。我没有提供完整的跟踪,因为它会带我们浏览整个函数名称的迁移堆栈。包括我的确切命令行在内的相关部分是:
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)
启动项目是可以访问连接字符串的项目,无论我指定上面的"Model"项目还是我实际的WPF启动项目,我的结果都是一样的。
好的 - 对于其他人来说,我已经解决了这个问题。
进一步思考后,不得不 为我的项目和 EF 迁移使用不同的连接字符串,否则消息没有意义。
提供给 EF 迁移的连接字符串是在我的自定义 DbContext class 的静态构造函数中设置的。就我而言,这是正确的行为。 但是 当项目被编译并且 运行 时,设置文件被读取并且我的代码向连接字符串添加了一些其他信息。
这就是两者之间存在差异的原因 - 在静态构造函数中将信息添加到 CS 修复了错误。
我的问题是我为包管理器控制台设置的环境 (ASPNETCORE_ENVIRONMENT) 与我的 .NET Core 项目不同。
在程序包管理器控制台中使用 Get-ChildItem Env:
列出您的环境变量。
我用Update-Database -Verbose
查看命令使用的是哪个环境
然后我更改了环境变量,重新启动 Visual studio,它使用了正确的项目设置。