EF Core 2.0 connection string in Azure Functions .NET Core

前端 未结 2 1804
终归单人心
终归单人心 2020-12-17 20:28

I\'m using EF core 2.0 in Azure Functions using .net core. I\'m trying to read db ConnectionString from local.settings.json, which is defined:

{
  \"IsEncryp         


        
相关标签:
2条回答
  • 2020-12-17 21:05

    You could use one of the helper classes in Microsoft.Azure.WebJobs.Host:

    AmbientConnectionStringProvider.Instance.GetConnectionString("MyDbConnStr")
    

    This class is delegating the work to internal class called ConfigurationUtility, which does something in line with

    var configuration = new ConfigurationBuilder()
        .AddEnvironmentVariables()
        .AddJsonFile("appsettings.json", true)
        .Build();
    var conn = configuration.GetConnectionString("MyDbConnStr");
    
    0 讨论(0)
  • 2020-12-17 21:13

    You can use Environment.GetEnvironmentVariable. The keys are "ConnectionStrings:YourKey"

    Assuming your local.settings.json looks like the following:

    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "AzureWebJobsDashboard": "UseDevelopmentStorage=true"
      },
      "ConnectionStrings": {
        "sqldb_connection": "connection_string_here"
      }
    }
    

    Use Environment.GetEnvironmentVariable("ConnectionStrings:sqldb_connection", EnvironmentVariableTarget.Process); to get the value

    0 讨论(0)
提交回复
热议问题