mirror of
https://github.com/fergalmoran/podnoms.git
synced 2025-12-23 01:40:26 +00:00
18 lines
809 B
C#
18 lines
809 B
C#
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using PodNoms.Api.Persistence;
|
|
|
|
namespace PodNoms.Api.Providers {
|
|
public static class WebHostExtensions {
|
|
public static IWebHost MigrateDatabase(this IWebHost webHost, bool migrate, bool dropFirst) {
|
|
var serviceScopeFactory = (IServiceScopeFactory)webHost.Services.GetService(typeof(IServiceScopeFactory));
|
|
using (var scope = serviceScopeFactory.CreateScope()) {
|
|
var services = scope.ServiceProvider;
|
|
var dbContext = services.GetRequiredService<PodNomsDbContext>();
|
|
if (dropFirst) dbContext.Database.EnsureDeleted();
|
|
if (migrate) dbContext.Database.EnsureCreated();
|
|
}
|
|
return webHost;
|
|
}
|
|
}
|
|
} |