首页 > 解决方案 > 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

一些代码快照

程序.cs 在此处输入图像描述

启动设置.json 在此处输入图像描述

更新:我尝试使用 IIS 和 Kestrel 服务器,但在这两种情况下都会出现这个问题。

标签: entity-framework-coreentity-framework-migrations

解决方案


在对这个问题进行了大量搜索之后,我发现了一件真正对我有帮助的事情。

当前可用的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();

推荐阅读