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.
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();
}
...
}