C# 从IDesignTimeDbContextFactory访问App.config<;T>;

C# 从IDesignTimeDbContextFactory访问App.config<;T>;,c#,entity-framework,entity-framework-core,ef-code-first,entity-framework-migrations,C#,Entity Framework,Entity Framework Core,Ef Code First,Entity Framework Migrations,为了通过Entity Framework Core执行迁移,我创建了一个设计时工厂,如中所述。目前,我已经实现了这一点,这似乎是可行的: public StudentContextCreateDbContext(string[] args) { string connectionString = "Data Source=localhost;Initial Catalog=MyCatalog;Integrated Security=True;"; var optionsBuild

为了通过Entity Framework Core执行迁移,我创建了一个设计时工厂,如中所述。目前,我已经实现了这一点,这似乎是可行的:

public StudentContextCreateDbContext(string[] args)
{
    string connectionString = "Data Source=localhost;Initial Catalog=MyCatalog;Integrated Security=True;";
    var optionsBuilder = new DbContextOptionsBuilder<StudentContext>();
    optionsBuilder.UseSqlServer(connectionString);
    return new StudentContext(optionsBuilder.Options);
}
它仅从
App.config
文件返回完全相同的连接字符串。它在实际应用程序中工作,但当我从设计时工厂使用它时,我得到一个
System.NullReferenceException


如何在应用程序和设计时工厂之间共享相同的连接字符串,而不手动维护两者?

EF Core在设计时实例化DB上下文,例如添加迁移或更新数据库,以收集有关实体的详细信息并映射它们。但是使用ConfigurationManager无法访问项目的App.config,因为EF core design time是另一个具有自己应用程序设置的应用程序

从我的答案中找到解决方案:

string connectionString = ConfigurationManager.ConnectionStrings["Local"].ConnectionString;