azure functions 7.0 dotnet-isolated read connection string (no startup class) to setup entity framework dbcontext

Eduard Popescu 0 Reputation points
2023-02-18T00:29:26.6266667+00:00
var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureAppConfiguration(x => x.AddEnvironmentVariables())
    .ConfigureHostConfiguration(x => x.AddEnvironmentVariables())
    .ConfigureServices(services =>
    {
        services.AddSingleton<HttpClient>();
        services.AddDbContext<EsiUniverseDbContext>(
            x =>
            {
                var connectionString = Environment.GetEnvironmentVariable("ConnectionString:EMS");
                x.UseSqlServer(connectionString);            });
...

In the local.settings.json I have

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
  },
  "ConnectionStrings": {
    "EMS": "<MY CONNECTION STRING>"
  }
}

Everything works locally. What should I write in the Environment.GetEnviromentVariable in order to read the connection string when deployed in Azure?

I have the connection string setup correctly in the Configuration section for the Function App in Azure.

I am not using a Startup class.
Developer technologies | .NET | Entity Framework Core
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,932 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2023-02-22T22:24:05.47+00:00

    zyrconium Thank you for posting your question in Microsoft Q&A. Sorry for the delay in response. I assume, you are looking to know how to set up connection string when deploying .NET 7 Isolated Function in Azure and your code snippet follows .NET sample available in GitHub https://github.com/Azure/azure-functions-dotnet-worker/tree/main/samples/EntityFramework.

    The settings defined in local.settings.json are not migrated automatically and as described in this doc, you would need to define these in Connection strings in your Azure Function App. Check Configure connection strings to define App settings and Connection strings and both can be read as environment variables with the same code snippet like you shared.

    From your note above, you have already defined connection string in the Configuration section. If so, then I would request to validate the format in accessing the environment variable since connection strings require specific formatting as described below (Use Environment.GetEnvironmentVariable("SQLCONNSTR_EMS") for your example reference):

    User's image

    Instead of defining connection strings, you can also use app settings to avoid special formatting as described in the docs. I hope this helps with your question and feel free to add a comment for any questions or face any issues. Would be happy to answer any.


    Please accept as "Yes" if the answer is helpful so that it can help others in the community.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.