Код: Выделить всё
builder.Services.AddDbContext((_, options) =>
{
// some configuration
}, ServiceLifetime.Transient);
builder.Services.AddDbContext((_, options) =>
{
// the exact same configuration
}, ServiceLifetime.Transient);
// and also
using var scope = app.Services.CreateScope();
using var context1 = scope.ServiceProvider.GetRequiredService();
context1.Database.Migrate();
using var context2 = scope.ServiceProvider.GetRequiredService();
context2.Database.Migrate();
// etc.
Мне бы хотелось у меня есть возможность каждый раз перебирать все DbContexts, так что вероятность того, что я забуду зарегистрировать один из них, меньше. Примерно так:
Код: Выделить всё
public static void DoWithEachRegisteredDbContext(DoWithDbContext magic)
{
magic();
magic();
}
public delegate Type[] DoWithDbContext();
// and this method is called like this for the above example
DoWithEachRegisteredDbContext(AddDbContext);
public Type[] AddDbContext() {
builder.Services.AddDbContext((_, options) =>
{
// some configuration
}, ServiceLifetime.Transient);
DoWithEachRegisteredDbContext(Migrate);
public Type[] Migrate() {
using var scope = app.Services.CreateScope();
using var context = scope.ServiceProvider.GetRequiredService();
context.Database.Migrate();
}
Возможно ли что-то подобное? Как?
Подробнее здесь: https://stackoverflow.com/questions/790 ... -parameter