Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

412
Visualizações
dotnet entity framework migration: how to set connection string environment variable, with colon, on MacOS

I'm using EntityFramework in .NET to handle DB migrations. For example, dotnet ef migrations list --project My.Project --context MyDbContext.

However, some of these entity framework commands require a connection string, to connect to the DB and see what's what:

    Build started...
    Build succeeded.
    Args are:
    Schema Name: myschema
    Connection string: 
    Connection string not set up. No connection string works when adding a migration (EF compares to the snapshot.cs file) but you will need a connection string to apply a migration.
    Connection string not set up in environment variable: 'ConnectionString:SchemaAdmin'. Set the connection string in the Package Manager Console first: $env:ConnectionString:SchemaAdmin='<replace with connection string>'

So I need to set up an environment variable called ConnectionString:SchemaAdmin containing the connection string for my DB.

However, this is not easy, since there is a colon in the name of the environment variable:

    $ fo:o=bar
    bash: fo:o=bar: command not found
    $ fo\:o=bar; echo $foo
    bash: fo:o=bar: command not found

How am I supposed to do this?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The way that I found to resolve this was to use AddJsonFile in a builder at Startup (which allows it to find the configuration stored in the appsettings.json file) and then use that to set a private _config variable

public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        _config = builder.Build();
    }

And then I could set the configuration string as follows:

var connectionString = _config.GetConnectionString("DbContextSettings:ConnectionString"); 
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda