找不到EF核心工具System.Configuration.ConfigurationManager程序集

时间:2018-11-06 09:48:53

标签: entity-framework .net-core ef-core-2.1

我正在创建一个使用EF Core 2进行迁移的新应用程序。 应用程序本身是.net Framework,但是模型位于单独的.net core 2.0程序集中。一切正常,我已经定义了一个设计时上下文工厂:

public class MyDesignTimeContextFactory : IDesignTimeDbContextFactory<MyContext>
{
    public MyContext CreateDbContext(string[] args)
    {
        return new MyContext("Server=localhost\\SQLEXPRESS;Database=DBName;User ID=Test;Password=0000;");
    }
}

我可以生成迁移并将其应用/恢复到数据库。 现在,如果我将硬编码的连接字符串替换为对配置文件的调用

return new MyContext(System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString");

调用EF Core工具时出现错误:

Add-Migration -Project MyProject.Model -Name Initialization
System.IO.FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=4.0.1.0 ....,

但是这里有nuget,我可以在启动应用程序本身时毫无问题地访问ContextFactory中的ConfigurationManager(而不是设计时)。似乎EF核心工具没有在寻找模型装配的依赖项...

我缺少什么吗?也许无法在DesignTime上下文工厂中使用ConfigurationManager?

1 个答案:

答案 0 :(得分:0)

最后,问题出在应用程序项目中。我必须将System.Configuration.ConfigurationManager的nuget包添加到.Net Framework应用程序,以便PackatManager可以找到它。有点奇怪,它可以在运行时运行,但不能在“设计模式”下​​运行。

相关问题