提供 IDbContextFactory 的实现。但是哪里?

Provide an implementation of IDbContextFactory. But where?

我正在尝试 运行 使用自定义 DbContext 进行迁移。

var migrationConfiguration = new DbMigrationsConfiguration { AutomaticMigrationsEnabled = true };
migrationConfiguration.ContextType = typeof(DataContext);
var migrator = new DbMigrator(migrationConfiguration);
migrator.Update();

这会抛出迁移异常,因为DataContext没有实现无参数构造函数:

The target context 'System.Data.Entity.DbContext' is not constructible. Add a default constructor or provide an implementation of IDbContextFactory.

DataContext构造函数需要参数,但我已经创建了 IDbContextFactory<DataContext>。我如何告诉 DbMigrator 使用 IDbContextFactory<DataContext> 的现有实现?

IDbContextFactory<> 已在同一个程序集中并提供无参数 构造函数。

另一种方法是继承 DbConfiguration 并在派生 class 的 无参数 构造函数中设置工厂(我正在使用 MEF 从 IDbContextFactory 派生 class ):

public class DataContextConfiguration : DbConfiguration
{
    public DataContextConfiguration()
    {
        SetContextFactory(() => (DataContext)new CompositionManager().Container.GetExportedValue<IDataContextFactory>().Create());
    }
}