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(); await dbContext.Database.MigrateAsync(cancellationToken); } public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; }