bielu000
bielu000

Reputation: 2241

dotnet ef does not respect configuration

I have two configurations: - Development - Production

with following json settings files: - appsettings.Production.json - appsettings.Development.json

In each settings file I have following sections:

(production)

"Sql" : {
    "Name": "App_Prod",
    "Server": "127.0.0.1",
    "Port": 0,
    "Database": "db_prod",
    "Username": "user_prod",
    "Password": "secret"
   },

(development)

"Sql" : {
    "Name": "App_Dev",
    "Server": "127.0.0.1",
    "Port": 0,
    "Database": "db_dev",
    "Username": "user_dev",
    "Password": "secret"
   },

I'm injectings these into my DbContext constructor, and it works properly. I updated my database using migrations system using foolowing command:

dotnet ef database update --context MyDbContext--configuration Development

it worked, and my database scheme has been updated.

But when I'm trying to update my production database using following commanad:

dotnet ef database update --context MyDbContext--configuration Production

it gaves me information that any migrations no needs to be applied.

I dumped connection string in MyDbContext, and I can see that the DbContext still use Development configuration even I type that it should be use Production settings file.

Why migration mechanism does not respect my configuration? How I can run dotnet ef with appsettings.Production?

Upvotes: 7

Views: 5030

Answers (3)

Khayo
Khayo

Reputation: 33

On dotnet core 6+ you can use "-- --environment envName"

something like this:

dotnet ef database update --context MyDbContext -- --environment Prod

its weird but works

Upvotes: 3

bielu000
bielu000

Reputation: 2241

In my case(Linux environment) using:

export ASPNETCORE_ENVVIRONMENT=Development

changes environment used by database update command

Upvotes: 3

Moho
Moho

Reputation: 16498

--configuration is for build configuration (e.g. Release, Debug)

Pre-v2.0:

Use -e or --environment:

PM> dotnet ef database update --context MyDbContext -e Production

v2.0+:

Set ASPNETCORE_ENVIRONMENT environment variable before executing command:

PM> $env:ASPNETCORE_ENVIRONMENT='Production'

Upvotes: 6

Related Questions