部署到Azure - 无法连接到数据库

问题描述:

我最近订阅了Azure免费试用版,目前正在尝试发布我的网站。部署到Azure - 无法连接到数据库

然而,当我得到了我的网站上公布我遇到了以下错误:

[NullReferenceException: Object reference not set to an instance of an object.] 
MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection) +56 
    System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +276 
    System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) +27 
    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 
    System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest) +56 
    System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +43 
    System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +62 
    System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +123 
    System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +610 
    System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +18 
    System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +53 
    System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +15 
    System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +38 

当我与连接字符串到我的远程数据库,它工作正常运行这个地方,但只要我发布它不起作用。

我的连接字符串如下所示:

<add name="DefaultConnection" connectionString="user id=****;password=****;persistsecurityinfo=False;server=**** ;database=iprospect_tools" providerName="MySql.Data.MySqlClient" /> 
<add name="ToolsEntities" connectionString="metadata=res://*/ToolsModel.csdl|res://*/ToolsModel.ssdl|res://*/ToolsModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;user id=****;password=****;persistsecurityinfo=False;server=****;database=tools&quot;" providerName="System.Data.EntityClient" /> 

当我远程调试应用程序,这是错误的评价:

enter image description here

任何想法可能会导致这个错误?

+0

您的数据库和网站托管在Azure(哪个服务)在哪里?你是否能够在azure中从你的实例ping数据库。 –

+0

我的网站作为网络应用程序托管,我的数据库作为MySQL数据库托管。我不确定从Azure中的实例中ping数据库的位置 - 你能告诉我在哪里可以做到吗? –

+0

您可以通过kudu使用命令控制台。要访问kudu,请浏览https://sitename.scm.azurewebsites.net/DebugConsole。在这里,您可以使用tcpping.exe来ping特定端口上的主机名。 'tcpping hostname:port' –

如果网站在本地工作,并且在发布到Azure后数据库连接失败 - 在Azure网站设置中确实缺少配置。

enter image description here

查阅这些SO thread 1SO thread 2

有一些informatin丢失形成你的问题,这将使提供consice答案更容易,但这里是我会做什么:

  1. 找出数据库所在的位置

这里要做的第一件事就是弄清楚你用于MySQL数据库的服务。在Azure中,至少有四种方法来托管MySQL数据库,并且每个方法可能需要不同的步骤。

  • 在MySQL应用 因为你正在做从桌面的远程连接你使用这个选项,我不认为。如果你正在使用这个由ClearDB 提供

  • 的MySQL,那么服务器名称将是这样的:美国CDBR - 蔚蓝 - * cloudapp.net微软 提供如果

  • 的MySQL。您正在使用这一点,那么服务器名称将是这样的:*在IaaS的VM 如果你使用这个,然后托管.mysql.database.azure.com

  • 的MySQL服务器名称可能类似于:* .cloudapp.net

如果数据库未托管在Azure中,则应检查网络配置。

  1. 创建一个最简单的应用程序来测试连接。

假设你的数据库在Azure中托管的下一步是要减少的努力,以确定问题是否出在DB服务器,网络配置,你的代码,或其他依赖你的依赖。

string myConnectionString = "your-connectionstring-goes-here"; 
    try{ 
     using (var con = new MySqlConnection { ConnectionString = myConnectionString }){ 
      con.Open(); 

      if (!con.Ping()){ 
       System.Diagnostics.Trace.TraceError("MySQL Ping Failed:"); 
      } 
      else{ 
       System.Diagnostics.Trace.TraceInformation("MySQL Successfull"); 
      } 
     } 
    } 
    catch (Exception exep){ 
     System.Diagnostics.Trace.TraceError("Could not connect to MySQL: " + exep.Message); 
    } 

注意,在这个例子中,我们是硬编码的连接字符串到代码绕过甚至网络配置。

如果这是行得通的,那么我们知道与数据库的连接性好,应用服务应用可以访问数据库。跳过步骤(3)并转到步骤(4)

如果它不工作,那么我们仍然有更多的调试要做。转到步骤(3)

  1. 解决网络/数据库服务器

    • 防火墙如果你的数据库是由微软MySQL的托管检查以确保防火墙是让您的应用程序连接到它,并重复步骤(2)

    • 如果您的数据库被托管在IaaS的VM请确保端口和网络是开放的accepct连接并重复步骤(2)

  2. 一旦你步骤(2)工作,然后再次尝试使用你的代码。

    1. 调试代码
    2. 一旦你得到步骤(2)工作,那么排除了连接问题。这意味着该错误出现在您的代码,连接字符串配置或您使用Entity Framework的方式中。

      希望这有助于

    开始=>
开始=“3”>