【问题标题】:Azure Api/Web App with Entity Framework - SQL database connection string带有实体框架的 Azure Api/Web 应用程序 - SQL 数据库连接字符串
【发布时间】:2016-04-29 07:14:00
【问题描述】:

我正在向我的 Azure API 应用程序添加一个 SQL 数据库。我有一个空的 SQL 数据库,我通过 portal.azure.com 单独创建。我的问题是我不知道如何设置连接字符串以便我的应用使用 Azure 数据库。

我已关注Code First Migrations 文章,但我被困在部署阶段。我在项目的任何文件中都看不到任何连接配置。

如何设置应用在 Azure 中部署时使用的连接字符串?


更多信息:

确切地说,我可以看到两件事:

  1. 注释掉 Web.Debug/Release.config 文件中的 connectionStrings 部分。
  2. Web.Config 中的一些 EF 配置:

       <entityFramework>
            <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
              <parameters>
                <parameter value="mssqllocaldb" />
              </parameters>
            </defaultConnectionFactory>
           (...)
    

当我在本地执行测试时,我可以看到 Database.Connection.ConnectionString

Data Source=(localdb)\mssqllocaldb;Initial Catalog=XDataAPI.Models.MyContext;Integrated Security=True;MultipleActiveResultSets=True

顺便说一句。发布窗口指出在项目中没有找到数据库。 (这并没有真正困扰我,这是次要问题)


编辑:

DbContext,根据要求:

public class MyAppContext : DbContext 
    { 
        public DbSet<Organisation> Organisations { get; set; } 
    }

【问题讨论】:

  • 请显示您的 DbContext 类或至少 DbContextClass 构造函数的代码。
  • 上下文是超级基本的,就像在没有构造函数的教程中一样。
  • 我需要看看您是否以及如何将连接名称传递给您的基本构造函数 - 如果操作不正确,可能会导致您的问题。

标签: entity-framework azure azure-sql-database azure-api-apps


【解决方案1】:

将连接名称作为参数传递给您的构造函数,然后在 web.config 中设置连接字符串时使用相同的连接名称,如下所示:

public class MyAppContext : DbContext 
{ 
    public MyAppContext():base("MyConnectionName"){}
    public DbSet<Organisation> Organisations { get; set; } 
}

然后,在 web.config 中:

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="MyConnectionName" connectionString="Server=tcp:test.database.windows.net,1433;Database=testdb;User ID=test@test;Password=p4ssw0rd!;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
....
<configuration>

如果您想从本地计算机运行,请记住您需要在 Azure 数据库服务器防火墙上允许来自您的 IP 的传入连接。

【讨论】:

    【解决方案2】:

    如果您设置了 SQL Server VM,那么

    <add name="DataContext" connectionString="Data Source=VMname.cloudapp.net; Initial Catalog=catalog; User ID=login;Password=password; MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
    

    如果你设置了 SQL Azure,那么that tutorial should be used

    至于连接字符串的地方,请参考一些documentation。你使用 LocalDB,而不是你应该使用 SQL Server。

    【讨论】:

    • 我正在使用 SQL Azure。本教程只说从 portal.azure.com 复制连接字符串 - 我的问题是我不知道把它放在哪里。
    • 更新了我的答案。基本上,您现在使用 LocalDB。将其更改为 SQL Server 之一,您应该可以开始使用了。
    【解决方案3】:

    您应该能够在 web.config 中更新数据上下文的连接字符串以使用您的 Azure SQL 数据库。对于我的测试项目,它位于 web.config 的顶部:

    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <connectionStrings>
        <add name="WebApplication4Context" connectionString="Server=tcp:test.database.windows.net,1433;Database=testdb;User ID=test@test;Password=p4ssw0rd!;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
          providerName="System.Data.SqlClient" />
      </connectionStrings>
    ....
    <configuration>
    

    不要忘记更新您的 Azure SQL 数据库服务器的防火墙设置,以便您的应用程序可以访问它。

    编辑:您还可以通过在发布对话框中添加您的 Azure SQL DB 来更改您的 Azure 环境的数据库连接:

    【讨论】:

    • 谢谢,发布窗口报告项目中没有找到数据库:/
    • 在发布对话期间在此步骤添加 Azure 数据库时,您必须在此处手动添加连接字符串。您必须从 Azure 门户复制数据库的连接字符串并添加密码。然后,您可以在单击三个点“...”后检查您的连接。
    • 连接字符串没地方放——这就是问题所在。
    • 我添加了另一个截图。希望对您有所帮助。
    • 感谢您提供另一个屏幕截图,但“发布”屏幕中缺少该字段:(
    【解决方案4】:

    如果连接字符串缺少 web.config,则它使用默认名称 DefaultConnection,它指的是使用 SQL 或 SQL Express 安装的 localdb 实例。

    要配置它,您必须首先在 Azure 上创建一个 SQL DB,从门户,创建一个新数据库并为其命名,并确保它存在于同一资源组和区域中,以减少延迟并改善性能。

    打开创建的数据库,你会发现很多平台的连接字符串,复制一个.Net的并进入Web App设置,你应该找到一个连接字符串的地方,添加一个新的并将其命名为DefaultConnection和添加您刚刚从数据库刀片复制的连接字符串的值

    当您第一次运行应用程序时,如果您在发布向导中指定,代码首先将连接到数据库并应用迁移,这也会在 web.config 中添加一些配置。

    【讨论】:

      【解决方案5】:

      对于 .Net FW 4.5 或更高版本:
      1. 你的 DbContext 类:

      public class MyAppContext: DbContext
      {
          public MyAppContext() : base("YourConnectionStringKey") { }
      
          public DbSet<Organization> Organizations{ get; set; }
      }
      

      2。您的 Web.config:

      <connectionStrings>
          <add name="YourConnectionStringKey" connectionString="DummyValue" providerName="System.Data.SqlClient" />
        </connectionStrings>
      

      3。在您的 Azure WebApp 设置中,添加连接字符串(将在运行时自动注入到您的 Web.config 中)

      如果您不使用 .Net 框架进行开发,请参阅 https://azure.microsoft.com/en-us/blog/windows-azure-web-sites-how-application-strings-and-connection-strings-work/ 了解更多详情。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-31
        • 1970-01-01
        • 2018-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多