Run database migrations using Entity Framework ASP.NET Core on application start

asp.net asp.net-core entity-framework entity-framework-core

Question

I would like to automatically run a database migration on application start in ASP.NET Core 2.0 and EntityFramework Core 2.0.

I have found Run database migrations using Entity Framework core on application start. However, my connection strings are stored in the environment variables so they would not be found until the .AddEnvironmentVariables() are called in the Configure method.

How to call the db.Database.Migrate() or how to properly do the database migration (in Azure Web App) via Continuous Deployment and staging environments?

public class Startup
{
    private IConfigurationRoot Configuration { get; set; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddDbContext<ClientContext>(options => options.UseSqlServer(this.Configuration.GetConnectionString("AppService")));
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMvc();

        this.Configuration = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables()
            .Build();
    }

    // public void Configure(IApplicationBuilder app, ClientContext db)
    // {
    //     db.Database.Migrate();
    // }
}
1
3
7/19/2017 12:48:44 PM

Accepted Answer

Based on you comment above I would try this:

public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider)
{
    app.UseMvc();

    this.Configuration = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables()
        .Build();

    var db = serviceProvider.GetService<ClientContext>();
    db.Database.Migrate();
}

This is manually resolving the ClientContext after the Environment variables have been set up and therefore it won't fail the parameter injection.

EDIT: The error you're getting can be resolved by following the steps in this SO question

3
7/19/2017 1:34:49 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow