Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 390
bielu000 Avatar asked Jul 19 '26 01:07

bielu000


2 Answers

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

like image 89
Moho Avatar answered Jul 21 '26 14:07

Moho


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

something like this:

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

its weird but works

like image 29
Khayo Avatar answered Jul 21 '26 15:07

Khayo