123456789101112131415161718192021222324252627282930313233343536373839 |
- 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<HRIdentityDBContext>();
-
- await identityContext.Database.EnsureCreatedAsync(cancellationToken);
- await identityContext.Database.MigrateAsync(cancellationToken);
- var HRDataContext = scope.ServiceProvider.GetRequiredService<HRDataContext>();
- await HRDataContext.Database.EnsureCreatedAsync(cancellationToken);
- await HRDataContext.Database.MigrateAsync(cancellationToken);
- }
- }
- public Task StopAsync(CancellationToken cancellationToken)
- {
- return Task.CompletedTask;
- }
- }
|