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

24 lines
770 B
C#

using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
namespace Vegasco.Server.Api.Persistence;
public class ApplyMigrationsService(
ILogger<ApplyMigrationsService> logger,
IServiceScopeFactory scopeFactory,
ActivitySource activitySource)
: IHostedService
{
public async Task StartAsync(CancellationToken cancellationToken)
{
using var activity = activitySource.StartActivity("ApplyMigrations");
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;
}