Added initializer for dev environment for the new dbContexts
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ public static class DependencyInjection
|
||||
Action<DbContextOptionsBuilder>? configureAction = null)
|
||||
{
|
||||
services.AddDbContext<ContentDbContext>(configureAction);
|
||||
services.AddScoped<ContentDbContextInitializer>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user