I have a ASP.NET Core app with Docker support and following service configuration in the Startup.cs
:
string connectionString = Environment.GetEnvironmentVariable("ORACLE_CONNECTION_STRING");
services.AddDbContext<BloggingContext>(options => options.UseOracle(connectionString));
I set the connection string in the docker-compose.override.yml
:
version: '2'
services:
continuouseverything:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ORACLE_CONNECTION_STRING=Server=(DESCRIPTION = (ADDRESS = (PROTOCOL=TCP) (HOST=LOCALHOST) (PORT=1521)) (CONNECT_DATA= (SERVER=DEDICATED) (SERVICE_NAME=xe.oracle.docker))); direct=true; User Id=system; Password=oracle; Unicode=true
ports:
- "80"
It works fine when I just start the app via Docker. And in Debugger I can see that the connectionString
variable got the right connection String.
But when I try to run Add-Migration InitialCreate
I get an ArgumentNullException
in the Startup.cs
when services.AddDbContext()
is called because the connectionString
variable is null.
How do I accomplish this, that the EnvironmentVariable
is set like defined in the docker-compose
file?
EF Core version: Microsoft.EntityFrameworkCore.Tools (1.1.1)
Database Provider: Devart.Data.Oracle.EFCore (9.4.280)
IDE: Visual Studio 2017
@dapalmi the default ASP.NET Core templates are structured to make it easy to pull such application configuration data as connection strings from alternative sources: instead of calling Environment.GetEnvironmentVariable()
make sure you are calling AddEnvironmentVariables()
on the ConfigurationBuilder
. Then you can change the code in AddDbContext()
to be:
C#
services.AddDbContext<BloggingContext>(options =>
options.UseOracle(Configuration["ORACLE_CONNECTION_STRING"]));
Now it should be easy to specify a default connection string for migrations commands to use when the setting from docker-compose
file is not available, e.g. to have a connection string for development, you can put the connection string in appsettings.Development.json. To avoid ever checking in your connection string, you can take advantage of user secrets as an alternate source for the connection string. See https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets for more details.
Most helpful comment
@dapalmi the default ASP.NET Core templates are structured to make it easy to pull such application configuration data as connection strings from alternative sources: instead of calling
Environment.GetEnvironmentVariable()
make sure you are callingAddEnvironmentVariables()
on theConfigurationBuilder
. Then you can change the code inAddDbContext()
to be:C# services.AddDbContext<BloggingContext>(options => options.UseOracle(Configuration["ORACLE_CONNECTION_STRING"]));
Now it should be easy to specify a default connection string for migrations commands to use when the setting from
docker-compose
file is not available, e.g. to have a connection string for development, you can put the connection string in appsettings.Development.json. To avoid ever checking in your connection string, you can take advantage of user secrets as an alternate source for the connection string. See https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets for more details.