mirror of
https://github.com/fergalmoran/podnoms.git
synced 2025-12-22 09:18:08 +00:00
20 lines
849 B
C#
20 lines
849 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 isDevelopment) {
|
|
if (isDevelopment) {
|
|
var serviceScopeFactory = (IServiceScopeFactory)webHost.Services.GetService(typeof(IServiceScopeFactory));
|
|
using (var scope = serviceScopeFactory.CreateScope()) {
|
|
var services = scope.ServiceProvider;
|
|
var dbContext = services.GetRequiredService<PodNomsDbContext>();
|
|
// dbContext.Database.EnsureDeleted();
|
|
dbContext.Database.EnsureCreated();
|
|
}
|
|
}
|
|
return webHost;
|
|
}
|
|
}
|
|
} |