如何访问DbMigration中的AppSetting?

时间:2019-01-14 16:01:02

标签: c# asp.net entity-framework

我需要通过Up()函数访问其他项目中的appsetting。

我的问题是我无法在迁移中加载构造函数。

public partial class MyMigration : DbMigration {    
    public override void Up() {
         // Here I need access to my appSettings    
    } 
}

1 个答案:

答案 0 :(得分:0)

对此充满信心,而不是最佳实践。如果您想回退init应用程序的配置,还可能必须回滚。该链接也可以为您提供帮助:How to select different app.config for several build configurations

var path = @"C:\somepath\Configuration\App.config";
string confFolder = Path.GetDirectoryName(path);
var pathFilterRoot = Path.GetPathRoot(confFolder);
string configFileToOPen = Path.GetFileName(pathFilterRoot);

ExeConfigurationFileMap fileMapConfig = new ExeConfigurationFileMap  {
    ExeConfigFilename = Path.Combine(Path.GetFullPath(pathFilterRoot + "\\" + configFileToOPen))
};

System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMapConfig, ConfigurationUserLevel.None);