How to Update/Migrate SQLite database with EF on Azure

asp.net-core entity-framework-core sqlite

Question

I'm using an SQLite database for my application. On desktop I can easily run Add-Migration and Update-Database when needed.

Now that I'm further developing the database and features while the site is live I need to run Update-Database on the server, but I don't know how to do that.

Is there a piece of code that I can run during start up that will update the database?

Due to my subscription I don't have Azure Storage or access to the command line, so that's out of the option.

1
2
3/14/2018 6:32:50 AM

Accepted Answer

You can call DbContext.Database.Migrate() in Configure method of Startup class. This will automatically migrate your SQLite database on application start if there are pending migrations.

public void ConfigureServices(IServiceCollection serviceCollection)
{
    ...
    services.AddDbContext<MyDbContext>(options =>
            options.UseSqlite(...));
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    using(var context = new MyDbContext(...))
    {
        context.Database.Migrate();
    }
    ...
}
3
3/16/2018 5:24:27 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