【发布时间】:2015-12-10 14:08:56
【问题描述】:
我有一个简单的 ASP Web API 2 应用程序,它部署到 Azure Web 应用程序。它使用同样托管在 Azure 上的 SQL 数据库。 DbContext 是使用 Publish 对话框设置的,如下图所示,因此我信任 VS 工具,并且我假设连接字符串设置正确:(可以找到以下示例 here)
连接字符串在 Web.config 中设置
<connectionStrings>
<add name="DefaultConnection"
connectionString="..."
providerName="System.Data.SqlClient"/>
</connectionStrings>
并用于ctor的IdentityDbContext:
public ApplicationDbContext() : base("DefaultConnection")
当 API 项目在本地调试时,它可以毫无问题地访问生产数据库,但是一旦部署它就不能再访问它了。以下是例外情况(发回邮递员)。 SO上有一些关于类似问题的其他答案,但到目前为止我没有成功地关注它们。
[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
【问题讨论】:
-
您是否根据构建配置转换配置文件?
-
不,我禁用它们是为了调试这个问题。
-
如果您可以在生产环境中验证连接字符串是否正确,那么您的问题可能是您尝试访问的网络位置无法使用生产数据库?您确定 DNS,您的
hosts文件中没有指向数据库的条目吗? -
WebApp 和 Sql 数据库都是从 Azure 门户托管和管理的。是否应该授予某种权限?
标签: c# sql-server azure asp.net-web-api asp.net-web-api2