entity-framework-core - EF Core Tool 忽略 .net core 2.1 和 3.1 中的 launchSettings.json
问题描述
在 launchSettings.json 中,定义了一些变量,应用程序使用这些变量。当我正常运行应用程序时,launchSettings.json 中可用的环境变量成功加载并且应用程序运行完美。但是当我尝试添加迁移时,ef 工具会忽略 launchSettings.json 文件。
launchSettings.json 看起来像
{“MIN_LOG_LEVEL”:“警告”}
将 launchSettings.json 变量读取为的代码
config.AddEnvironmentVariables();
string logLevel = config["MIN_LOG_LEVEL"] // returing null
一些代码快照
更新:我尝试使用 IIS 和 Kestrel 服务器,但在这两种情况下都会出现这个问题。
解决方案
在对这个问题进行了大量搜索之后,我发现了一件真正对我有帮助的事情。
当前可用的EF Core 工具不自动支持launchSettings.json,如果我们想要获取 launchSettings.json 中存在的变量,那么我们需要手动解析文件。
CLI添加了对 launchSettings.json的支持,但到目前为止它仅适用于dotnet run命令。EF Core 工具无法自动获取 launchSettings.json 变量。
我从github issue得知后会尽快添加此功能
如果您想利用launchSettings.json,那么您可以按如下方式进行。
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "Properties", "launchSettings.json"), optional: false, reloadOnChange: true)
.Build();
推荐阅读
- python - 获取 N 维数组的所有索引作为列表
- ssl - javax.net.ssl.SSLHandshakeException:收到致命警报:WAS 8.5.5.9 中的握手失败
- vba - 从另一个工作簿中查找每天更改姓名的工作簿
- oracle - ora-28040 没有匹配的身份验证协议
- r - ggplot2 geom_bar:居中条
- angularjs - Bootstrap 确认模态窗口服务
- jsf - 有没有办法在primefaces中分离selectOneListbox中的项目
- three.js - 使用 three.js 移动对象
- java - 从netbenas中的MySQL表制作java实体类
- java - 在 Java 8 中使用流减少 bean 列表