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

Pet*_*ter 29 connection-string azure asp.net-core

我在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数据库的连接字符串,而不在源代码管理中检入它?

Update My Startup.cs看起来像这样(参见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;
}
Run Code Online (Sandbox Code Playgroud)

在该ConfigureServices方法中,还有:

services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));
Run Code Online (Sandbox Code Playgroud)

而且,在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
Run Code Online (Sandbox Code Playgroud)

Sha*_*tin 32

简短的回答

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

你很亲密

  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

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

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

在中配置数据库Startup.

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

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

  • 这有效.但是将"DefaultConnection"转换为"Data:DefaultConnection:ConnectionString"的魔力是什么? (2认同)

Sha*_*iro 11

在RC2中,我不得不改变连接字符串的读取方式,以使它们在Azure中工作.在我的情况下,我必须确保Azure连接字符串被命名为"DefaultConnection",并通过以下方式访问:

RC1:

{
    "Data": {
        "DefaultConnection": {
            "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=db;Trusted_Connection=True;"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

访问:

var conn = Configuration["Data:DefaultConnection:ConnectionString"];
Run Code Online (Sandbox Code Playgroud)

RC2:

{
  "Data": {

  },
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=db;Trusted_Connection=True;"
  }
}
Run Code Online (Sandbox Code Playgroud)

访问:

var conn = Configuration.GetConnectionString("DefaultConnection");
Run Code Online (Sandbox Code Playgroud)