在 Azure 中为 ASP.NET Core Web 应用设置 SQL 连接字符串 [英] Setting the SQL connection string for ASP.NET Core web app in Azure

查看:28
本文介绍了在 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"作为键.

(顺便说一下设置App Settings好像不行.我觉得我提供的值太长了).

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

更新我的 Startup.cs 看起来像这样(请参阅 完整的GitHub 上的文件):

公共启动(IHostingEnvironment env){var 配置 = 新配置().AddJsonFile("config.json").AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);如果(env.IsEnvironment(开发")){配置.AddUserSecrets();}configuration.AddEnvironmentVariables();配置=配置;}

ConfigureServices方法中,还有:

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

而且,也在 ConfigureServices 方法中:

services.AddEntityFramework().AddSqlServer().AddDbContext(选项=>options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])).AddDbContext(options =>options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));//所以这是我希望 Azure 中的应用程序使用连接字符串的地方//在门户中提供

解决方案

简短回答

<块引用>

我已尝试在门户中设置应用程序设置,包括连接字符串和应用程序设置.并同时使用SQLCONNSTR_DefaultConnection"和Data:DefaultConnection:ConnectionString"作为键.

你很接近.

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

示例使用 timesheet_db 而不是 DefaultConnection

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

Azure 网络应用配置

Azure Web 应用服务控制管理器

https://myWebAppName.scm.azurewebsites.net/Env 将显示连接字符串.

Startup.cs

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

public IConfiguration Configuration { get;放;}公共启动(){配置 = 新配置().AddJsonFile("config.json").AddEnvironmentVariables();<----- 将级联到 config.json}

Startup中配置数据库.

public void ConfigureServices(IServiceCollection services){服务.AddEntityFramework().AddSqlServer().AddDbContext(选项=>{var connString =Configuration.Get("数据:timesheet_db:ConnectionString");options.UseSqlServer(connString);});}

当然,该示例使用名为 timesheet_db 的连接字符串.对您来说,用您自己的名为 DefaultConnection 的连接字符串替换它的所有实例,一切都会正常运行.

I have created a new ASP.NET Core web application in Visual Studio 2015. I've also set up an Azure web app to pull in the app from GitHub and run it. This works fine, but I'm having trouble connecting to the database on Azure.

Locally, this works, and it uses config.json and in code Data:DefaultConnection:ConnectionString for the connection string.

How can I leave the code as it is, and have it work in Azure too? 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.

(Setting the App Settings doesn't seem to work by the way. I think the value I provide is too long).

So how can I provide the connection string for my Azure database to my Azure web app (ASP.NET 5), without checking it in in source control?

Update My Startup.cs looks like this (see 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;
}

In the ConfigureServices method, there is also:

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

and, also in the ConfigureServices method:

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

解决方案

Short answer

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.

You're close.

  1. Go to Azure web app > configure > connection strings.
  2. Add a connection string with the name DefaultConnection.
  3. Use Configuration.Get("Data:DefaultConnection:ConnectionString") to access it.

Example using timesheet_db instead of DefaultConnection

This is an example from my own timesheet application. My connection string was named timesheet_db. Just replace all instances of that string with DefaultConnection to adapt the example to your use case.

Azure web app configuration

Azure web app service control manager

The online service control manager at https://myWebAppName.scm.azurewebsites.net/Env will show the connection strings.

Startup.cs

Setup configuration settings in Startup so that the environmental variables overwrite the config.json.

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

Configure the database in Startup.

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

Of course, the example uses a connection string named timesheet_db. For you, replace all instances of it with your own connection string named DefaultConnection and everything will work.

这篇关于在 Azure 中为 ASP.NET Core Web 应用设置 SQL 连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆