using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using MTWorkHR.Identity.DBContext; using MTWorkHR.Infrastructure.Data; 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); var HRDataContext = scope.ServiceProvider.GetRequiredService(); await HRDataContext.Database.EnsureCreatedAsync(cancellationToken); await HRDataContext.Database.MigrateAsync(cancellationToken); } } public Task StopAsync(CancellationToken cancellationToken) { return Task.CompletedTask; } }