public static void Main(string[] args) { var webHost = CreateWebHostBuilder(args).Build(); // 初始化DB DBInitializer.Initialize(webHost); CoreMapper.Initialize(); webHost.Run(); }
public static void Initialize(IWebHost host) { using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var context = services.GetRequiredService <CoreDBContext>(); var logDBContext = services.GetRequiredService <BusinessDBContext>(); var webAppOptions = services.GetRequiredService <IOptionsMonitor <WebAppOptions> >(); DBInitializer.Initialize(context, logDBContext, webAppOptions.CurrentValue); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <DBInitializer> >(); logger.LogError(ex, "初始化数据库异常."); } } }