Added initializer for dev environment for the new dbContexts

This commit is contained in:
Dominic Villemure
2024-08-18 11:06:32 -04:00
parent f5f80fa234
commit 558fe7bc67
5 changed files with 68 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
namespace Hutopy.Web.Features.Contents.Data;
public static class InitializerExtensions
{
public static async Task InitialiseContentDbContextAsync(this WebApplication app)
{
using var scope = app.Services.CreateScope();
var initializer = scope.ServiceProvider.GetRequiredService<ContentDbContextInitializer>();
await initializer.InitialiseAsync();
}
}
public class ContentDbContextInitializer(
ILogger<ContentDbContextInitializer> logger,
ContentDbContext context
)
{
public async Task InitialiseAsync()
{
try
{
await context.Database.MigrateAsync();
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred while initialising the content database.");
throw;
}
}
}