I have created an extension method called AddSQLServer that is used in the Startup.cs / ConfigureServices that simply adds the dbContext to the ServiceCollection. I would like to run the migrations directly after initializing the dbContext.
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
var connectionString = Configuration.GetConnectionString("Test");
services.AddLogging(config =>
{
config.AddSqlServer(connectionString);
});
}
Action method which configures the dbContext
public class MyProvider
{
private readonly IServiceCollection serviceCollection;
public MyProvider(IServiceCollection serviceCollection)
{
this.serviceCollection = serviceCollection;
}
public void AddSqlServer(string connectionString)
{
serviceCollection.AddDbContext<OutputDbContext>(config => config.UseSqlServer(connectionString));
// somehow calling dbContext.Database.Migrate(); to run migrations
}
}