Function App, Entity Framework, .Net 6 and environment variables

Viewed 361

When updating an Azure Functions application that is using Entity Framework (code first) to .Net 6, none of the values from local.settings.json are found. This problem appears when running an "ef" command, but not when debugging or running the application.

The Azure Functions application is configured to run in isolated mode, and retrieves values from the settings file with the Environment.GetEnvironmentVariable method. And this works perfect when debugging or running the application.

// Simplified code example
public static async Task Main(string[] args)
{
  var builder = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(s =>
    {
      var connectionString = Environment.GetEnvironmentVariable("ConnectionString");
      // connectionString is null here only when running dotnet ef command
    });
  builder.RunAsync();
}

But if I run any ef command from the terminal (such as dotnet ef database update) all values are null so it seems like when running these commands the local.settings.json are not taken in consideration.

This worked fine when running from .Net 5. The EntityFrameworkCore NuGet packages has of course also been updated to support the new .Net version properly.


Update

To clarify, the local.settings.json file is simple and structured this way. In .Net 5 I can easily retrieve variables from the "Values" section, but after updating to .Net 6 they are always null when running dotnet ef commands.

{
    "IsEncrypted": false,
    "Values": {
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
        "ConnectionString": "[...]"
    },
    "Host": {
        "CORS": "*",
        "CORSCredentials": false
    }
}
0 Answers
Related