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

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

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

如何保留代码,并使其在Azure中运行?我已经尝试在门户中设置应用程序设置,包括连接字符串和应用程序设置。并使用“sqlCONNSTR_DefaultConnection”和“Data:DefaultConnection:ConnectionString”作为键。

(设置应用程序设置似乎不起作用。我认为我提供的值太长)。

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

更新
我的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

解决方法

简短的回答

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.

你很亲密

>转到Azure Web应用程序>配置>连接字符串。
>添加名为DefaultConnection的连接字符串。
>使用Configuration.Get(“Data:DefaultConnection:ConnectionString”)访问它。

使用timesheet_db而不是DefaultConnection的示例

这是我自己的时间表应用程序中的一个示例。我的连接字符串名为timesheet_db。只需用DefaultConnection替换该字符串的所有实例,以使示例适应您的用例。

Azure Web应用程序配置

Azure Web应用程序服务控制管理器

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

Startup.cs

在“启动”中设置配置设置,以便环境变量覆盖config.json。

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

在“启动”中配置数据库

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的连接字符串替换它的所有实例,一切都会起作用。

相关文章

本文将从上往下,循序渐进的介绍一系列相关.NET的概念,先从...
基于 .NET 的一个全新的、好用的 PHP SDK + Runtime: Pe...
.NET 异步工作原理介绍。
引子 .NET 6 开始初步引入 PGO。PGO 即 Profile Guided Opti...
前言 2021/4/8 .NET 6 Preview 3 发布,这个版本的改进大多来...
前言 开头防杠:.NET 的基础库、语言、运行时团队从来都是相...