📄 MatDenDagen/Infrastructure/Storage/Database/StartupMigration.cs
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace MatDenDagen.Infrastructure.Storage.Database;

public class StartupMigration(IServiceScopeFactory scopeFactory) : IHostedService
{
    public async Task StartAsync(CancellationToken cancellationToken)
    {
        using var scope = scopeFactory.CreateScope();
        var questionnaireContext = scope.ServiceProvider.GetRequiredService<QuestionnaireContext>();
        await questionnaireContext.Database.MigrateAsync(cancellationToken);
    }

    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}