'Azure Function App Unable to get Connection String : Value cannot be null. (Parameter 'connectionString')

I have an Azure Function with DDD Architecture. My project structure looks like this: enter image description here

local.settings.json file Looks like this:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "ServiceBusConnectionString": "Endpoint=sb://sb.servicebus.windows.net/;*****"
  },
  "ConnectionStrings": {
    "DefaultConnection": "Server=tcp:*************"
  }
}

And my appsettings.json looks like this:

{
  "ConnectionStrings": {
     "DefaultConnection": "*******"
  }
}

And ApplicationDbContextFactory file looks like this :

public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
    {
        public ApplicationDbContext CreateDbContext(string[] args)
        {
            IConfigurationRoot configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();

            var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
            return new ApplicationDbContext(optionsBuilder.Options);
        }
    }


Solution 1:[1]

You need the specify the connection string prefix (see documentation):

Environment.GetEnvironmentVariable("CUSTOMCONNSTR_DefaultConnection");

This prefix classification is:

CUSTOMCONNSTR_ => Custom provider

MYSQLCONNSTR_ => MySQL

SQLAZURECONNSTR_ => Azure SQL Database

SQLCONNSTR_ => SQL Server

Credit goes to the people in this post:

Get Connection String in Azure Function v3

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 henda79