Provide an implementation of IDbContextFactory. But where?

I am trying to perform migrations using a custom DbContext.

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

This eliminates the migration exception because it DataContextdoes not implement the constructor without parameters:

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

The constructor DataContextrequires parameters, but I already have IDbContextFactory<DataContext>. How to tell DbMigrator to use an existing implementation IDbContextFactory<DataContext>?

+4
source share
1 answer

IDbContextFactory<> .

DbConfiguration factory ( MEF , IDbContextFactory):

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

All Articles