简体   繁体   English

使用Appsettings.json中定义的不同连接字符串进行CodeFirst迁移

[英]CodeFirst migrations with different connection strings defined in appsettings.json

In my asp.Net core (2.2) project, I've got 在我的asp.Net core(2.2)项目中,

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. 我对模型进行了更改,然后运行Add-Migration然后运行update-database但这仅更新开发服务器上的架构。

How do I get it to target / update the staging and production servers using the connections defined in the environmental appsettings.json files? 如何使用环境appsettings.json文件中定义的连接来定位/更新登台服务器和生产服务器?

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!? 我曾尝试在不同的环境中构建它,并且在不同的环境中运行时,我知道连接字符串已正确使用-但是如何使EF指向特定的数据库!

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 然后,您只需要运行dotnet ef migration cli命令

Update make sure you already set your enviroment config like this in startup.cs 更新以确保您已经在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 您可以在这里查看我的完整配置

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

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