简体   繁体   English

具有ASP NET CORE和Docker支持的基于环境的连接字符串

[英]Environment based connection string with ASP NET CORE and Docker support

I have a ASP.NET Core app with Docker support and following service configuration in the Startup.cs : 我有一个具有Docker支持并在Startup.cs以下服务配置的ASP.NET Core应用程序:

        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 : 我在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"

I works fine when I just start the app via Docker. 通过Docker启动应用程序时,我可以正常工作。 And in Debugger I can see that the connectionString variable got the right connection String. 在调试器中,我可以看到connectionString变量获得了正确的连接字符串。

But when I try to run Add-Migration InitialCreate I get an ArgumentNullException in the Startup when services.AddDbContext() is called because the connectionString variable is null. 但是,当我尝试运行Add-Migration InitialCreate时,由于connectionString变量为null,因此在调用services.AddDbContext()时在启动中得到ArgumentNullException

How do I accomplish this, that the EnvironmentVariable is set like defined in the docker-compose file? 我如何做到这一点,如在docker-compose文件中定义的那样设置EnvironmentVariable

如果直接从Powershell运行Add-Migration InitialCreate ,则需要首先在Shell中设置环境变量。

$env:ORACLE_CONNECTION_STRING = "connstring goes here"; Add-Migration InitialCreate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM