Небольшие фиксы

This commit is contained in:
Sergey Karmanov 2024-06-20 00:25:09 +03:00
parent 34eaf01df0
commit a3f26ec9f2
2 changed files with 2 additions and 30 deletions

View File

@ -26,35 +26,7 @@ public static class DbHelpersEndpointsExtensions
await dbContext.Database.MigrateAsync(); await dbContext.Database.MigrateAsync();
return Results.Ok(); return Results.Ok();
}); });
group.MapPost("/initdb/{contexClassName}", async (string contexClasstName, IServiceProvider services) =>
{
var contextType = GetEfCoreClassContexts().FirstOrDefault(c => c.Name == contexClasstName);
if(contextType is null)
return Results.NotFound();
using var scope = services.CreateScope();
var dbContext = (DbContext)scope.ServiceProvider.GetRequiredService(contextType);
await dbContext.Database.EnsureCreatedAsync();
return Results.Ok();
});
group.MapPost("/initdb", async (IServiceProvider services) =>
{
foreach(var contextType in GetEfCoreClassContexts())
{
using var scope = services.CreateScope();
var dbContext = (DbContext)scope.ServiceProvider.GetRequiredService(contextType);
await dbContext.Database.EnsureCreatedAsync();
}
return Results.Ok();
});
group.MapPost("/migrate", async (IServiceProvider services) => group.MapPost("/migrate", async (IServiceProvider services) =>
{ {
foreach(var contextType in GetEfCoreClassContexts()) foreach(var contextType in GetEfCoreClassContexts())

View File

@ -21,7 +21,7 @@ public static class ReflectionHelpers
//Get all types with DbContext parent type //Get all types with DbContext parent type
EfCoreDbContexts = typeof(Program).Assembly.GetTypes().Where(t => EfCoreDbContexts = typeof(Program).Assembly.GetTypes().Where(t =>
t.GetInheritanceChain().Any(inheritanType => t.GetInheritanceChain().Any(inheritanType =>
inheritanType.Name is not null && inheritanType.Name == nameof(DbContext))); !string.IsNullOrEmpty(inheritanType.Name) && inheritanType.Name == nameof(DbContext)));
} }
return EfCoreDbContexts; return EfCoreDbContexts;
} }