Files
vegasco/src/Vegasco.Server.Api/Persistence/ApplyMigrationsService.cs

24 lines
793 B
C#
Raw Normal View History

2024-08-24 13:43:43 +02:00
using Microsoft.EntityFrameworkCore;
2025-06-12 19:12:38 +02:00
using System.Diagnostics;
2024-08-24 13:43:43 +02:00
namespace Vegasco.Server.Api.Persistence;
2024-08-24 13:43:43 +02:00
2025-06-12 19:12:38 +02:00
public class ApplyMigrationsService(
ILogger<ApplyMigrationsService> logger,
IServiceScopeFactory scopeFactory,
ActivitySource activitySource)
: IHostedService
2024-08-24 13:43:43 +02:00
{
2025-06-12 19:12:38 +02:00
public async Task StartAsync(CancellationToken cancellationToken)
{
2025-06-24 19:28:55 +02:00
using Activity? activity = activitySource.StartActivity("ApplyMigrations");
2024-08-24 13:43:43 +02:00
2025-06-12 19:12:38 +02:00
logger.LogInformation("Starting migrations");
using IServiceScope scope = scopeFactory.CreateScope();
2025-06-24 19:28:55 +02:00
await using ApplicationDbContext dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
2025-06-12 19:12:38 +02:00
await dbContext.Database.MigrateAsync(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
2024-08-24 13:43:43 +02:00
}