I have an Azure Function with DDD Architecture. My project structure looks like this:
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);
}
}
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: