50 lines
1.2 KiB
C#
50 lines
1.2 KiB
C#
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using Microsoft.Extensions.Logging;
|
||
|
|
|
||
|
|
namespace CMSMicroservice.Infrastructure.Persistence;
|
||
|
|
|
||
|
|
public class ApplicationDbContextInitialiser
|
||
|
|
{
|
||
|
|
private readonly ApplicationDbContext _context;
|
||
|
|
private readonly ILogger<ApplicationDbContextInitialiser> _logger;
|
||
|
|
|
||
|
|
public ApplicationDbContextInitialiser(ApplicationDbContext context, ILogger<ApplicationDbContextInitialiser> logger)
|
||
|
|
{
|
||
|
|
_context = context;
|
||
|
|
_logger = logger;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task InitialiseAsync()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (_context.Database.IsSqlServer())
|
||
|
|
{
|
||
|
|
await _context.Database.MigrateAsync();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
_logger.LogError(ex, "An error occurred while initialising the database.");
|
||
|
|
throw;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task SeedAsync()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
await TrySeedAsync();
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
_logger.LogError(ex, "An error occurred while seeding the database.");
|
||
|
|
throw;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
public async Task TrySeedAsync()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|