簡體   English   中英

帶有實體框架的Azure Api / Web應用-SQL數據庫連接字符串

[英]Azure Api/Web App with Entity Framework - SQL database connection string

我正在向我的Azure API應用添加一個SQL數據庫。 我有一個空的SQL數據庫,該數據庫是通過portal.azure.com單獨創建的。 我的問題是我不知道如何設置連接字符串,以便我的應用程序使用Azure數據庫。

我已經閱讀了“代碼優先遷移”一文,但仍處於部署階段。 我在項目中的任何文件中都看不到任何連接配置。

當應用程序部署在Azure中時,如何設置connectionString由應用程序使用?


更多信息:

確切地說,我可以看到兩件事:

  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; } 
    }

將連接名稱作為參數傳遞給構造函數,然后在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的傳入連接。

如果設置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, 則應使用該教程

至於連接字符串的地方,請參考一些文檔 您使用LocalDB,而不是應該使用SQL Server。

您應該只需要在web.config中更新數據上下文的連接字符串即可使用Azure SQL數據庫。 對於我的testproject,它只是在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門戶

連接字符串

對於.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中) Azure WebApp設置

如果您不使用.Net框架進行開發,請參見https://azure.microsoft.com/zh-cn/blog/windows-azure-web-sites-how-application-strings-and-connection-strings-work /了解更多詳情。

如果連接字符串缺少web.config,則它將使用默認名稱DefaultConnection ,它引用隨SQL或SQL Express安裝的localdb實例。

若要對其進行配置,必須首先在Azure上從Portal創建一個SQL DB,創建一個新數據庫並為其命名,並確保它存在於相同的資源組和區域中,以減少延遲並提高性能。

打開創建的數據庫,您將找到許多平台的連接字符串,將其復制為.Net並轉到Web App設置,您應該找到一個連接字符串的位置,添加一個新名稱並將其命名為DefaultConnection並添加值您剛剛從數據庫刀片復制的連接字符串

首次運行該應用程序時,如果您在發布向導期間指定了代碼(它也會在web.config中添加一些配置),則代碼將首先連接到數據庫並應用遷移。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM