简体   繁体   中英

CodeFirst migrations with different connection strings defined in appsettings.json

In my asp.Net core (2.2) project, I've got

appsettings.production.json

appsettings.staging.json

appsettings.development.json

each with their own connection strings for different servers. I make a change to the models and run Add-Migration then update-database - but this only updates the schema on the development server.

How do I get it to target / update the staging and production servers using the connections defined in the environmental appsettings.json files?

I've tried building it using the different environments and when running in the different environments I know the connection strings are being used correctly - but how do I get EF to target specific databases!?

You can set specfic enviroment using this command like this blog mention

setx ASPNETCORE_ENVIRONMENT "Development"

Then you just need to run dotnet ef migration cli command

Update make sure you already set your enviroment config like this in startup.cs

builder
    .SetBasePath(hostingEnvironment.ContentRootPath)
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
    .AddEnvironmentVariables();

You can view my full config here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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