C# 在Azure中为ASP.NET核心web应用设置SQL连接字符串

C# 在Azure中为ASP.NET核心web应用设置SQL连接字符串,c#,azure,asp.net-core,connection-string,C#,Azure,Asp.net Core,Connection String,我在Visual Studio 2015中创建了一个新的ASP.NET核心web应用程序。我还设置了一个Azure web应用程序,从GitHub中拉入该应用程序并运行它。这工作正常,但我在连接Azure上的数据库时遇到问题 在本地,这是可行的,它使用config.json并在codeData:DefaultConnection:ConnectionString中使用连接字符串 我怎样才能让代码保持原样,让它也在Azure中工作?我已尝试在门户中设置应用程序设置,包括连接字符串和应用程序设置。并

我在Visual Studio 2015中创建了一个新的ASP.NET核心web应用程序。我还设置了一个Azure web应用程序,从GitHub中拉入该应用程序并运行它。这工作正常,但我在连接Azure上的数据库时遇到问题

在本地,这是可行的,它使用
config.json
并在code
Data:DefaultConnection:ConnectionString
中使用连接字符串

我怎样才能让代码保持原样,让它也在Azure中工作?我已尝试在门户中设置应用程序设置,包括连接字符串和应用程序设置。并使用“SQLCONNSTR_DefaultConnection”和“Data:DefaultConnection:ConnectionString”作为键

(顺便说一下,设置应用程序设置似乎不起作用。我认为我提供的值太长了)

那么,我如何在不在源代码管理中将Azure数据库的连接字符串签入的情况下,将其提供给我的Azure web应用程序(ASP.NET 5)

更新 我的Startup.cs如下所示(请参阅):

ConfigureServices
方法中,还有:

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

您有许多选项可以设置连接字符串。 默认设置类从不同来源获取环境设置。 您可以在config.production.json中设置连接字符串。或config.staging.json。查看startup类

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

我想你在找我

在Azure PowerShell中使用此命令可将2个应用程序设置设置为与插槽粘滞

Set-AzureWebsite -Name mysite -SlotStickyAppSettingNames @("myslot", "myslot2")
Set-AzureWebsite -Name mysite -SlotStickyConnectionStringNames @("myconn", "myconn2")
此命令用于将2个连接字符串设置为与插槽粘滞

Set-AzureWebsite -Name mysite -SlotStickyAppSettingNames @("myslot", "myslot2")
Set-AzureWebsite -Name mysite -SlotStickyConnectionStringNames @("myconn", "myconn2")
简短回答 我已尝试在门户中设置应用程序设置,包括连接字符串和应用程序设置。并使用“SQLCONNSTR_DefaultConnection”和“Data:DefaultConnection:ConnectionString”作为键

你很接近

  • 转到Azure web app>配置>连接字符串
  • 添加名为
    DefaultConnection
    的连接字符串
  • 使用
    Configuration.Get(“Data:DefaultConnection:ConnectionString”)
    访问它
  • 使用
    时间表\u db
    而不是
    DefaultConnection
    这是我自己的时间表应用程序中的一个示例。我的连接字符串名为
    时间表\u db
    。只需将该字符串的所有实例替换为
    DefaultConnection
    ,以使示例适应您的用例

    Azure web应用程序配置

    Azure web应用服务控制管理器 位于的联机服务控制管理器将显示连接字符串

    Startup.cs 在
    Startup
    中设置配置设置,以便环境变量覆盖config.json

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

    当然,该示例使用名为
    timesheet\u db
    的连接字符串。对你来说,用你自己的名为
    DefaultConnection
    的连接字符串替换它的所有实例,一切都会正常工作。

    在RC2中,我必须更改我的连接字符串的读取方式,才能让它们在Azure中工作。在我的情况下,我必须确保Azure连接字符串名为“DefaultConnection”,并由以下人员访问:

    var conn = Configuration["Data:DefaultConnection:ConnectionString"];
    
    var conn = Configuration.GetConnectionString("DefaultConnection");
    
    RC1:

    访问人:

    var conn = Configuration["Data:DefaultConnection:ConnectionString"];
    
    var conn = Configuration.GetConnectionString("DefaultConnection");
    
    RC2:

    访问人:

    var conn = Configuration["Data:DefaultConnection:ConnectionString"];
    
    var conn = Configuration.GetConnectionString("DefaultConnection");
    

    我知道,但我不想将config.production.json添加到我的公共GitHub存储库中。据我所知,您可以在Azure门户(read)中指定设置和连接字符串。但是我无法让它工作。你能分享一下你的Startup.cs配置条目的样子吗?如果你已经正确设置了所有内容,那么在azure网站中指定
    数据:DefaultConnection:ConnectionString
    应该已经工作了。你的示例代码没有显示你是如何访问ConnectionString的。您在哪里访问连接字符串?语法是什么。您还可以通过在VS中的“项目属性->调试”下设置环境变量来本地测试该环境变量。这有助于排除故障(即,您正在设置环境变量名并将其设置为相同的名称)。@GeraldDavis我添加了有关如何访问连接字符串的代码。我将尝试有关项目属性的提示。这很有效。但是,将“DefaultConnection”转换为“Data:DefaultConnection:ConnectionString”的魔法是什么?魔法存在于环境VariableConfigurationProvider中,例如check。顺便说一句,RC2中的约定正在改变。配置键的形式为
    ConnectionStrings:name
    configuration。在最终版本中,使用该配置名称约定的Get
    不再有效。我们需要使用
    Configuration.GetConnectionString(“timesheet\u db”)