Merged PR 103: Added initializer for dev environment for the new dbContexts
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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace Hutopy.Web.Features.Messages.Data;
|
||||
|
||||
public static class InitializerExtensions
|
||||
{
|
||||
public static async Task InitialiseMessagingDbContextAsync(this WebApplication app)
|
||||
{
|
||||
using var scope = app.Services.CreateScope();
|
||||
|
||||
var initializer = scope.ServiceProvider.GetRequiredService<MessagingDbContextInitializer>();
|
||||
|
||||
await initializer.InitialiseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public class MessagingDbContextInitializer(
|
||||
ILogger<MessagingDbContextInitializer> logger,
|
||||
MessagingDbContext context
|
||||
)
|
||||
{
|
||||
public async Task InitialiseAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
await context.Database.MigrateAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "An error occurred while initialising the messaging database.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ public static class DependencyInjection
|
||||
Action<DbContextOptionsBuilder>? configureAction = null)
|
||||
{
|
||||
services.AddDbContext<MessagingDbContext>(configureAction);
|
||||
services.AddScoped<MessagingDbContextInitializer>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -109,6 +109,8 @@ app.UseAuthorization();
|
||||
|
||||
// Initialize and seed the db.
|
||||
await app.InitialiseApplicationDatabaseAsync();
|
||||
await app.InitialiseContentDbContextAsync();
|
||||
await app.InitialiseMessagingDbContextAsync();
|
||||
await app.SeedDatabaseWithTestDataOnlyIfNoDataIsPresentAsync();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
||||
Reference in New Issue
Block a user