Api/Brizco.Identity.Api/Services/DbInitializerService.cs

38 lines
1.1 KiB
C#

namespace Brizco.Identity.Api.Services;
public class DbInitializerService : IDbInitializerService
{
private readonly ApplicationContext _context;
private readonly ILogger<DbInitializerService> _logger;
public DbInitializerService( ApplicationContext context, ILogger<DbInitializerService> logger)
{
_context = context;
_logger = logger;
}
public void Initialize()
{
try
{
_context.Database.Migrate();
_logger.LogInformation("Migration SUCCESS !!!!");
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
}
}
public static void InitilizeDB(IApplicationBuilder app)
{
var scopeFactory = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>();
using (var scope = scopeFactory.CreateScope())
{
var identityDbInitialize = scope.ServiceProvider.GetService<IDbInitializerService>();
if (identityDbInitialize != null)
{
identityDbInitialize.Initialize();
}
}
}
}