ASP.NET Core 2.1由于环境变量而无法运行迁移 [英] ASP.NET Core 2.1 Unable to run migrations due to environment variable

查看:50
本文介绍了ASP.NET Core 2.1由于环境变量而无法运行迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我项目的结构:

  • App.UI
  • App.Data(ClassLibrary项目)

App.UI 包含一些我希望 App.Data 可以访问的环境变量,例如连接字符串.

App.UI contains some environment variables that I would like App.Data to have access to such as the connection string.

现在的问题是,当我尝试进行这样的迁移时:

The problem now is that when I try to do a migration like this:

Add-Migration AppOneBaseMigration -Context AppDbContext -OutputDir Migrations\AppOneMigrations

我收到此错误:

System.ArgumentNullException:值不能为null.参数名称:路径

System.ArgumentNullException: Value cannot be null. Parameter name: path

这是我运行迁移时初始化的代码:

This is the code that initialises when I run my migrations:

public AppDbContext CreateDbContext(string[] args)
{
    //Debugger.Launch();
    IConfigurationRoot configuration = new ConfigurationBuilder()
    .SetBasePath(ConfigurationManager.GetBasePath(Environment.GetEnvironmentVariable("CENTRAL_APPLICATION_SETTINGS")))
    .AddJsonFile("mssettings.json")
    .Build();
    var builder = new DbContextOptionsBuilder<AppDbContext>();
    builder.UseSqlServer(configuration["DatabaseConfiguration:ConnectionString"]);
    return new AppDbContext(builder.Options);
}

还有其他方法可以将 Environment.GetEnvironmentVariable("CENTRAL_APPLICATION_SETTINGS")变量传递给它吗?

Is there another way I can pass the Environment.GetEnvironmentVariable("CENTRAL_APPLICATION_SETTINGS") variable to it?

我尝试在 App.Data 中设置环境变量,但没有将其选中.

I tried setting an environment variable in App.Data but it didn't pick it up.

推荐答案

在运行Add-Migration命令之前,您应该可以在Package Manager控制台中设置变量,方法是键入

You should be able set the variable in the Package Manager Console before running the Add-Migration command by typing

$env:CENTRAL_APPLICATION_SETTINGS="your value"

然后运行

Add-Migration AppOneBaseMigration -Context AppDbContext -OutputDir Migrations\AppOneMigrations

这篇关于ASP.NET Core 2.1由于环境变量而无法运行迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆