using Microsoft.EntityFrameworkCore; using System.Diagnostics; namespace Vegasco.Server.Api.Persistence; public class ApplyMigrationsService( ILogger logger, IServiceScopeFactory scopeFactory, ActivitySource activitySource) : IHostedService { public async Task StartAsync(CancellationToken cancellationToken) { using Activity? activity = activitySource.StartActivity("ApplyMigrations"); logger.LogInformation("Starting migrations"); using IServiceScope scope = scopeFactory.CreateScope(); await using ApplicationDbContext dbContext = scope.ServiceProvider.GetRequiredService(); await dbContext.Database.MigrateAsync(cancellationToken); } public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; }