Efcore: Environment based connection string with Docker support and Code-First Migration

Created on 14 Jun 2017  路  1Comment  路  Source: dotnet/efcore

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 connectionStringvariable got the right connection String.

But when I try to run Add-Migration InitialCreate I get an ArgumentNullExceptionin the Startup.cswhen services.AddDbContext() is called because the connectionStringvariable is null.

How do I accomplish this, that the EnvironmentVariableis set like defined in the docker-composefile?

EF Core version: Microsoft.EntityFrameworkCore.Tools (1.1.1)
Database Provider: Devart.Data.Oracle.EFCore (9.4.280)
IDE: Visual Studio 2017

closed-question

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 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.

>All comments

@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.

Was this page helpful?
0 / 5 - 0 ratings