C# 运行EF 7迁移时指定HostingEnvironment
我已经根据我的应用程序环境为我的DBContext设置了连接字符串。所以在我的Startup.cs中C# 运行EF 7迁移时指定HostingEnvironment,c#,entity-framework,powershell,entity-framework-core,C#,Entity Framework,Powershell,Entity Framework Core,我已经根据我的应用程序环境为我的DBContext设置了连接字符串。所以在我的Startup.cs中 public Startup(IHostingEnvironment env, IApplicationEnvironment app) { Configuration = new ConfigurationBuilder(app.ApplicationBasePath) .AddJsonFile("config.json") .AddJsonFile($
public Startup(IHostingEnvironment env, IApplicationEnvironment app)
{
Configuration = new ConfigurationBuilder(app.ApplicationBasePath)
.AddJsonFile("config.json")
.AddJsonFile($"config.{env.EnvironmentName}.json", false)
.AddEnvironmentVariables()
.Build();
}
这个配置被注入到我的dbcontext中,如下所示
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命令
总之,在运行这些命令时,是否可以通过命令指定我的主机环境,或者执行任何其他类型的变通方法来指定我的环境?在即将发布的EF版本中,迁移如何发现迁移服务将发生变化
这是WIP。请参阅EF7的Wiki-和添加了--environment选项。这似乎不起作用。我的
Startup.cs
有一个名为ConfigureDevelopmentServices
的方法,当我使用Kestrel运行时,它会正确执行该方法,而不是ConfigureServices
。但是,当我运行dnx ef migrations add Foo
时,它会执行ConfigureServices
。指定--环境开发
无法解决此问题。