所以我一直在努力解决这个问题。在Update-Database
中运行Package Manager Console
时,得到以下输出。
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception (0x80004005): The network path was not found
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, SqlAuthenticationProviderManager sqlAuthProviderManager)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
... more errors ...
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
ClientConnectionId:00000000-0000-0000-0000-000000000000
Error Number:53,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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
我已验证我可以连接到SQL Server Management Studio中的RDS数据库,也可以在Visual Studio本身的Server Explorer中打开它。我也可以毫无问题地运行Enable-Migrations
。以下是我的连接字符串以及初始化DBContext
的位置。连接字符串是我在Visual Studio中连接到数据库时提供的字符串。
web.config
<connectionStrings>
<add name="dbname" connectionString="Data Source=dbname.xxxxxxxxxxxx.us-east-2.rds.amazonaws.com;Initial Catalog=dbname;User ID=username;Password=password;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" providerName="System.Data.SqlClient" />
</connectionStrings>
<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>
Context.cs
public class ApplicationContext : DbContext
{
public DbSet<Team> Teams {get; set;}
}
我之前也做过类似的项目,但是自动调整适应数据库的连接字符串并没有太多问题。我确实相信我已经调整了RDS实例的安全组,以启用公共访问权限,并为所有安全组提供了访问权限。
任何帮助将不胜感激,如果需要更多信息,我将很乐意提供。我也能够很好地构建和运行项目。