50 lines
1.7 KiB
C#
50 lines
1.7 KiB
C#
using System;
|
|
using InternshipSystem.Repository;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Internal;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace InternshipSystem.Api.Extensions
|
|
{
|
|
public static class ApplicationBuilderExtensions
|
|
{
|
|
public static IApplicationBuilder UseMigration(this IApplicationBuilder app)
|
|
{
|
|
bool done = false;
|
|
while (!done)
|
|
{
|
|
try
|
|
{
|
|
using var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
|
using var context = serviceScope.ServiceProvider.GetService<InternshipPractiseSupervisorDbContext>();
|
|
|
|
context.Database.Migrate();
|
|
|
|
done = true;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
|
|
return app;
|
|
}
|
|
|
|
public static IApplicationBuilder UseDefaultData(this IApplicationBuilder app, bool useDefaultData)
|
|
{
|
|
using var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
|
using var context = serviceScope.ServiceProvider.GetService<InternshipPractiseSupervisorDbContext>();
|
|
var filler = serviceScope.ServiceProvider.GetService<DatabaseFiller>();
|
|
|
|
context.Database.Migrate();
|
|
if (useDefaultData && !context.Editions.Any())
|
|
{
|
|
filler.FillAll().Wait();
|
|
}
|
|
|
|
return app;
|
|
}
|
|
}
|
|
} |