消防培训系统服务器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.3 KiB

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AX.FireTrainingSys
{
/// <summary>
/// IHost 扩展类。
/// </summary>
public static class IHostExtensions
{
/// <summary>
/// 设置数据库。
/// </summary>
/// <param name="host"></param>
/// <returns></returns>
public static IHost SetupDatabase(this IHost host)
{
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
using (var context = services.GetRequiredService<DriveDbContext>())
{
try
{
context.Database.EnsureCreated();
context.SeedDatabase();
}
catch (Exception e)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(e, "在设置数据库过程中发生了错误!");
}
}
}
return host;
}
}
}