【发布时间】:2021-08-14 06:31:35
【问题描述】:
我有一个 .NET 5.0 应用程序,我根据环境使用不同的 appsettings 文件,以便为我的 SQL 数据库提供不同的连接字符串。
例如:
appsettings.Development:
"ConnectionStrings": {
"DefaultConnection": "Server=<development server connection string>"
},
appsettings.Production:
"ConnectionStrings": {
"DefaultConnection": "Server=<production server connection string>"
},
在我的 startup.cs 中:
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
我在 Mac 上使用 vscode。当我使用 dotnet run 运行我的网站时,它错误地使用了生产 appsettings,但是当我使用 Debug 选项时,它使用了正确的 Development appsettings。
我该如何解决这个问题?
【问题讨论】:
-
查看项目中
launchSettings.json文件中的变量ASPNETCORE_ENVIRONMENT。 -
这就是问题所在,我没有launchsettings.json。我以为我可以将环境变量添加到 launch.json 但事实证明我也需要 launchsettings.json
标签: .net entity-framework asp.net-core