Use the configuration depending on the selected database provider #13

Merged
Wesser merged 33 commits from feat/database-persistence into feat/add-setup 2024-06-01 05:45:18 +03:00
Showing only changes of commit 271df127a6 - Show all commits

View File

@ -36,8 +36,28 @@ public static class DependencyInjection
services.AddDbContext<SpecificWeekDbContext>(options => services.AddDbContext<SpecificWeekDbContext>(options =>
UseDatabase(options).CreateDbContext<SpecificWeekDbContext, SpecificWeek>(dbProvider)); UseDatabase(options).CreateDbContext<SpecificWeekDbContext, SpecificWeek>(dbProvider));
services.AddDbContext<UberDbContext>(options =>
{
var providerNamespace = typeof(Mark).Namespace + "." + Enum.GetName(dbProvider);
services.AddDbContext<UberDbContext>(DbConfig); var assembly = Assembly.GetExecutingAssembly();
var configurationTypes = assembly.GetTypes()
.Where(t =>
t is { IsNested: false, IsAbstract: false, Namespace: not null } &&
t.Namespace.StartsWith(providerNamespace) &&
t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>)));
var modelBuilder = new ModelBuilder();
foreach (var configurationType in configurationTypes)
{
var configurationInstance = Activator.CreateInstance(configurationType)!;
modelBuilder.ApplyConfiguration(configurationInstance);
}
var dbContext = (UberDbContext)Activator.CreateInstance(typeof(UberDbContext), (DbContextOptions<UberDbContext>)UseDatabase(options).Options)!;
dbContext.ApplyConfigurations(modelBuilder);
});
services.AddScoped<ICampusDbContext>(provider => provider.GetRequiredService<CampusDbContext>()); services.AddScoped<ICampusDbContext>(provider => provider.GetRequiredService<CampusDbContext>());
services.AddScoped<IDisciplineDbContext>(provider => provider.GetRequiredService<DisciplineDbContext>()); services.AddScoped<IDisciplineDbContext>(provider => provider.GetRequiredService<DisciplineDbContext>());