feat: add DbContext for UberDbContext

This commit is contained in:
Polianin Nikita 2024-05-30 20:20:20 +03:00
parent 7c79f7d840
commit 271df127a6

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>());