using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using MTWorkHR.Infrastructure.DBContext; namespace MTWorkHR.Application.StartupService; public class DbMigrationService : IHostedService { private readonly IServiceProvider sp; public DbMigrationService(IServiceProvider serviceProvider) { sp = serviceProvider; } public async Task StartAsync(CancellationToken cancellationToken) { using (var scope = sp.CreateScope()) { var identityContext = scope.ServiceProvider.GetRequiredService(); // await identityContext.Database.EnsureCreatedAsync(cancellationToken); await identityContext.Database.MigrateAsync(cancellationToken); } } public Task StopAsync(CancellationToken cancellationToken) { return Task.CompletedTask; } }