My issue is that I have a profile created that holds the environment variables that change throughout environments.
I have a profile:
"AppProfile": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DB_CONNECTION": "<connection-string>",
// ... more settings
}
}
When I access these variables on build, I use a class with these methods:
public static string DbConnection =>
GetEnvironmentVariable(nameof(EnvironmentVariableConstants.DB_CONNECTION));
private static string GetEnvironmentVariable(string variable)
{
return Environment.GetEnvironmentVariable(variable, EnvironmentVariableTarget.Process) ?? "";
}
Everything runs fine when I build the project from that profile. But when I run EF Core's Add-Migration <migration-name>
, an empty string is passed for my variable in Program.cs
.
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(EnvironmentVariables.DbConnection);
});
What could be wrong here?
I've tried setting the environment variable in nuget package manager console, and that enabled me to do a migration, but I feel like there's a better way to get this going.
We use Azure app services and set the environment variables for things like connection strings and API keys.