C# Asp Web Api部署的数据库连接

C# Asp Web Api部署的数据库连接,c#,sql-server,azure,asp.net-web-api,asp.net-web-api2,C#,Sql Server,Azure,Asp.net Web Api,Asp.net Web Api2,我有一个简单的ASP Web API 2应用程序,它被部署到Azure Web应用程序中。它使用的是同样托管在Azure上的SQL数据库。DbContext是使用发布对话框设置的,如下图所示,因此我信任VS工具,并且我假设连接字符串设置正确:(可以找到下面的示例) 连接字符串是在Web.config上设置的 <connectionStrings> <add name="DefaultConnection" connectionString="...

我有一个简单的ASP Web API 2应用程序,它被部署到Azure Web应用程序中。它使用的是同样托管在Azure上的SQL数据库。
DbContext
是使用发布对话框设置的,如下图所示,因此我信任VS工具,并且我假设连接字符串设置正确:(可以找到下面的示例)

连接字符串是在Web.config上设置的

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString="..." 
         providerName="System.Data.SqlClient"/>
</connectionStrings>
当API项目在本地调试时,它可以毫无问题地访问生产数据库,但一旦部署,它就不能再访问它了。以下是例外情况(发送回邮递员)。关于类似的问题,还有一些其他的答案,但到目前为止,我没有成功地遵循这些答案

[Win32Exception (0x80004005): No such host is known]

[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: TCP Provider, error: 0 - No such host is known.)]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +92
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +285
System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover, SqlAuthenticationMethod authType) +372
System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +172

[ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.]
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +271
System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) +33

[ProviderIncompatibleException: An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure.]
System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) +246
System.Data.Entity.Infrastructure.<>c__DisplayClass1.<ResolveManifestToken>b__0(Tuple`3 k) +32
System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) +72
System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection connection) +251
[Win32Exception(0x80004005):不知道这样的主机]
[SqlException(0x80131904):与网络相关或特定于实例的
建立与SQL Server的连接时出错
找不到服务器或无法访问服务器。请验证实例
名称正确,并且SQL Server配置为允许远程访问
连接。(提供程序:TCP提供程序,错误:0-不知道此类主机。)]
System.Data.SqlClient.SqlInternalConnection.OneError(SqlException异常,布尔断开连接,操作'1 wrapCloseInAction)+92
System.Data.SqlClient.TdsParser.throweexception和warning(TdsParserStateObject stateObj,布尔调用者连接锁,布尔异步关闭)+285
System.Data.SqlClient.TdsParser.Connect(ServerInfo ServerInfo,sqlinternalconnectionds connHandler,Boolean ignoresnopentimeout,Int64 timerExpire,Boolean encrypt,Boolean trustServerCert,Boolean integratedSecurity,Boolean with failover,SqlAuthenticationMethod authType)+372
System.Data.SqlClient.sqlinternalconnectionds.AttemptOneLogin(ServerInfo ServerInfo、String newPassword、SecureString newSecurePassword、Boolean ignoresnopentimeout、TimeoutTimer timeout、Boolean with failover)+172
[ProviderIncompatibleException:提供程序未返回ProviderManifestToken字符串。]
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection连接)+271
System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices,DbConnection connection)+33
[ProviderIncompatibleException:访问数据库时出错。这通常意味着与数据库的连接失败。请检查连接字符串是否正确,以及是否正在使用适当的DbContext构造函数指定该字符串或在应用程序的配置文件中查找该字符串。请参阅http://go.microsoft.com/fwlink/?LinkId=386386有关DbContext和连接的信息。有关失败的详细信息,请参阅内部异常。]
System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices,DbConnection连接)+246
System.Data.Entity.Infrastructure.c_uuuDisplayClass1.b_uuu0(元组'3K)+32
System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey,Func`2 valueFactory)+72
System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection连接)+251
验证您的实时连接字符串是否正确 转到并选择调试控制台PowerShell。然后运行此命令查看您的实时连接字符串

cd site/wwwroot
$xml = [xml](Get-Content Web.config)
$connStrings = $xml.SelectSingleNode("configuration/connectionStrings") 
$connStrings.InnerXml
验证防火墙规则是否正确 转到,打开数据库服务器(浏览所有Sql servermyServer),然后选择设置。在防火墙下确保允许访问Azure服务已启用

您也可以从Azure PowerShell执行此操作,如下所示:

Get-AzureAccount                                # sign-in to your account
Get-AzureResource -ResourceGroupName mvp2015 `
    -ResourceName mvp2015 `
    -ResourceType Microsoft.Sql/servers/firewallrules `
    -OutputObjectFormat New
如果您看到一个名为
AllowalWindowsAzureIPS
的规则,那么您已经正确配置了

检查您的Web应用程序配置 您可能正在覆盖Web应用程序配置中的Web.config

您可以在portal.azure.com>Browse All>MyAppName>Settings>Application Settings>Connection strings上检查此项。如果有一个名为
DefaultConnection
的连接,它将覆盖Web.config连接字符串

cd site/wwwroot
$xml = [xml](Get-Content Web.config)
$connStrings = $xml.SelectSingleNode("configuration/connectionStrings") 
$connStrings.InnerXml
您还可以在myappname.scm.azurewebsites.net>环境>连接字符串中检查此项。确保此处列出的
DefaultConnection
符合您的要求


是否根据生成配置转换配置文件?否,我已禁用它们以调试此问题。如果您可以验证生产环境中的连接字符串是否正确,则问题可能是生产数据库在您尝试访问它的网络位置不可用?您确定DNS,您的
主机
文件中没有指向DB的条目吗?WebApp和Sql数据库都是从Azure portal托管和管理的。应该被授予某种权限吗?谢谢你的提示。不幸的是,字符串连接是正确的,权限已设置。实际上,
myappname.scm.azurewebsites.net
连接字符串中有一个旧的DefaultConnection。哪个Web应用配置设置了此连接字符串?通常您可以在portal.azure.com>浏览所有>MyAppName>设置>应用程序设置>连接字符串中找到连接字符串覆盖。