Starting Database Migration Using the Entity Framework Kernel at Application Launch

Is it possible to configure StartUp.cs or project.json to start database migration using Entity Framework Core at application startup?

Now I have middleware that performs this task, but seems to have a negative impact on performance because it checks the database for every request received.

 public class EntityFrameworkUpdateDatabaseMiddleware { private readonly RequestDelegate _next; private readonly ApplicationDbContext _dbContext; public EntityFrameworkUpdateDatabaseMiddleware(RequestDelegate next, ApplicationDbContext dbContext) { _next = next; _dbContext = dbContext; } public async Task Invoke(HttpContext context) { await _dbContext.Database.MigrateAsync(); await _next.Invoke(context); } } 
+3
source share
1 answer

This can be done in the configuration methods in Startup.cs . The easiest way:

 public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(); // add other services } public void Configure(IApplicationBuilder app, ApplicationDbContext db) { db.Database.Migrate(); // configure other services } 
+9
source

All Articles