I\'m using Entity Framework 5 with Code First Migrations. I have a DataStore class which derives from DbContext:
public class DataS
For those for whom upgrading to Entity Framework 6 is viable, there's a new overload of the migration initialization that makes this much easier:
// Parameters:
// useSuppliedContext:
// If set to true the initializer is run using the connection information from the
// context that triggered initialization. Otherwise, the connection information
// will be taken from a context constructed using the default constructor or registered
// factory if applicable.
public MigrateDatabaseToLatestVersion(bool useSuppliedContext);
Using this, you can run migrations with an injected DbContext as follows:
Database.SetInitializer(new MigrateDatabaseToLatestVersion(useSuppliedContext: true));
using (var context = kernel.Get())
context.Database.Initialize(false);