Use the configuration depending on the selected database provider #13
@ -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>());
|
||||||
|
Loading…
Reference in New Issue
Block a user