我已根据我的应用程序环境为我的DbContexts设置了连接字符串。所以在我的Startup.cs我有运行EF 7迁移时指定主机环境
public Startup(IHostingEnvironment env, IApplicationEnvironment app)
{
Configuration = new ConfigurationBuilder(app.ApplicationBasePath)
.AddJsonFile("config.json")
.AddJsonFile($"config.{env.EnvironmentName}.json", false)
.AddEnvironmentVariables()
.Build();
}
这种配置被注入到我的DbContexts如下
public MyDbContext(IConfiguration configuration)
{
Configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
var connString = Configuration.Get("SqlDb:ConnectionString");
builder.UseSqlServer(connString);
}
这样我可以用我的项目在不同的环境,我请(在设置应用ASPNET_ENV或主机设置)
但是,当我运行ef迁移(出于显而易见的原因)我根本不能指定HostingEnvironment时,启动类会查找名为“config..json”的文件,因为缺少环境名称。有没有办法解决这个问题或我可以做的解决方法?现在,无论何时运行迁移,我必须在运行迁移时对连接字符串进行硬编码。
为了感兴趣,我使用dnx从powershell运行迁移。 ef命令
总而言之,是否可以通过命令指定我的主机环境,或者在运行这些命令时执行任何其他类型的解决方法来指定我的环境?
这似乎不起作用。我的'Startup.cs'有一个名为'ConfigureDevelopmentServices'的方法,当我用Kestrel运行时,它正确地执行该方法而不是'ConfigureServices'。但是,当我运行'dnx ef migrations add Foo'时,它会执行'ConfigureServices'。指定'--environment Development'不能解决问题。 –