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

24 lines
770 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)
{
using var 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();
await using var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await dbContext.Database.MigrateAsync(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
2024-08-24 13:43:43 +02:00
}