在 Azure 中为 ASP.NET Core Web 应用程序设置 SQL 连接字符串

Setting the SQL connection string for ASP.NET Core web app in Azure

我在 Visual Studio 2015 年创建了一个新的 ASP.NET 核心 Web 应用程序。我还设置了一个 Azure Web 应用程序以从 GitHub 和 运行它。这工作正常,但我无法连接到 Azure 上的数据库。

在本地,这有效,它使用 config.json 并在代码中使用 Data:DefaultConnection:ConnectionString 作为连接字符串。

如何让代码保持原样,并使其在 Azure 中也能正常工作?我已经尝试在门户中设置应用程序设置,包括连接字符串和应用程序设置。并使用 "SQLCONNSTR_DefaultConnection" 和 "Data:DefaultConnection:ConnectionString" 作为键。

(顺便说一句,设置App Settings似乎不起作用。我觉得我提供的值太长了)。

那么我如何才能将我的 Azure 数据库的连接字符串提供给我的 Azure Web 应用程序 (ASP.NET 5),而无需在源代码管理中签入它?

更新 我的 Startup.cs 看起来像这样(参见 the full file on GitHub):

public Startup(IHostingEnvironment env)
{
    var configuration = new Configuration()
        .AddJsonFile("config.json")
        .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);

    if (env.IsEnvironment("Development"))
    {
        configuration.AddUserSecrets();
    } 

    configuration.AddEnvironmentVariables();
    Configuration = configuration;
}

ConfigureServices方法中,还有:

services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));

并且,也在 ConfigureServices 方法中:

services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options => 
                   options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]))
            .AddDbContext<InvoicesDbContext>(options => 
                   options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
// So this is where I want my app in Azure to use the connection string I
// provide in the portal

您可以通过多种方式设置连接字符串。 默认设置 class 从不同来源获取环境设置。 您可以在 config.production.json 中设置连接字符串。或 config.staging.json。查看启动class

    public Startup(IHostingEnvironment env)
    {
        // Setup configuration sources.
        var configuration = new Configuration()
            .AddJsonFile("config.json")
            .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);

        if (env.IsEnvironment("Development"))
        {
            // This reads the configuration keys from the secret store.
            // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
            configuration.AddUserSecrets();
        }
        configuration.AddEnvironmentVariables();
        Configuration = configuration;
    }

我想你在找 SlotSticky Settings

在 Azure PowerShell 中使用此命令将 2 个应用设置设置为粘附到插槽

Set-AzureWebsite -Name mysite -SlotStickyAppSettingNames @("myslot", "myslot2")

此命令将 2 个连接字符串设置为粘在插槽中

Set-AzureWebsite -Name mysite -SlotStickyConnectionStringNames @("myconn", "myconn2")

简答

I've tried setting the Application Settings in the portal, both the Connection Strings and the App Settings. And using both "SQLCONNSTR_DefaultConnection" and "Data:DefaultConnection:ConnectionString" as the key.

你很接近。

  1. 转到 Azure Web 应用 > 配置 > 连接字符串。
  2. 添加名称为 DefaultConnection 的连接字符串。
  3. 使用Configuration.Get("Data:DefaultConnection:ConnectionString")访问它。

示例使用 timesheet_db 而不是 DefaultConnection

这是我自己的时间表应用程序的示例。我的连接字符串被命名为 timesheet_db。只需将该字符串的所有实例替换为 DefaultConnection 即可使示例适应您的用例。

Azure Web 应用配置

Azure Web 应用服务控制管理器

https://myWebAppName.scm.azurewebsites.net/Env 的在线服务控制管理器将显示连接字符串。

Startup.cs

Setup 配置设置 Startup 以便环境变量覆盖 config.json。

public IConfiguration Configuration { get; set; }
public Startup()
{
    Configuration = new Configuration()
        .AddJsonFile("config.json")
        .AddEnvironmentVariables();    <----- will cascade over config.json
}

Startup中配置数据库。

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<ProjectContext>(options =>
        {
            var connString =
                Configuration.Get("Data:timesheet_db:ConnectionString");
            options.UseSqlServer(connString);
        });
}

当然,该示例使用名为timesheet_db 的连接字符串。对你来说,用你自己的名为 DefaultConnection 的连接字符串替换它的所有实例,一切都会起作用。

在 RC2 中,我必须更改读取连接字符串的方式才能使它们在 Azure 中工作。在我的例子中,我必须确保 Azure 连接字符串被命名为 "DefaultConnection",并通过以下方式访问:

RC1:

{
    "Data": {
        "DefaultConnection": {
            "ConnectionString": "Server=(localdb)\MSSQLLocalDB;Database=db;Trusted_Connection=True;"
        }
    }
}

访问者:

var conn = Configuration["Data:DefaultConnection:ConnectionString"];

RC2:

{
  "Data": {

  },
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\MSSQLLocalDB;Database=db;Trusted_Connection=True;"
  }
}

访问者:

var conn = Configuration.GetConnectionString("DefaultConnection");