Thursday, February 23, 2023

Run migration automatically in Production (.Net 6)

 


public static class MigrationManager
{
public static WebApplication MigrateDatabase(this WebApplication webApp)
{
using (var scope = webApp.Services.CreateScope())
{
using (var appContext = scope.ServiceProvider.GetRequiredService<ApplicationContext>())
{
try
{
appContext.Database.Migrate();
}
catch (Exception ex)
{
//Log errors or do anything you think it's needed
throw;
}
}
}
return webApp;
}

Followers

Link