Apply migrations on startup

This commit is contained in:
2024-08-24 13:43:43 +02:00
parent d3d3675e3d
commit 4a1f1a5a67
2 changed files with 24 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore;
namespace Vegasco.WebApi.Persistence;
public class ApplyMigrationsService : IHostedService
{
private readonly IServiceScopeFactory _scopeFactory;
public ApplyMigrationsService(IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
using IServiceScope scope = _scopeFactory.CreateScope();
await using var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await dbContext.Database.MigrateAsync(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}