Skip to content

Instantly share code, notes, and snippets.

@ZacharyCouchman
Last active February 11, 2019 09:39
Show Gist options
  • Save ZacharyCouchman/c0287e08d280299a3148c93f72b9e4a2 to your computer and use it in GitHub Desktop.
Save ZacharyCouchman/c0287e08d280299a3148c93f72b9e4a2 to your computer and use it in GitHub Desktop.
Adding the below line into the ConfigureServices method of the Startup.cs file will call EF migrations to run on app startup.
using Microsoft.EntityFrameworkCore;
...
public void ConfigureServices(IServiceCollection services)
{
// Add other services here
// Add the database context to the services collection for dependency injection
services.AddDbContext<MyDatabaseContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DatabaseConnectionString")));
// Get the database context and apply the migrations
var context = services.BuildServiceProvider().GetService<MyDatabaseContext>();
context.Database.Migrate();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment