public static void RunExternalCode(IHost host) { /* 启动时初始化数据库(如果需要) */ using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var config = services.GetRequiredService <IConfiguration>(); bool needDbInit = Convert.ToBoolean(config.GetSection("InitTask")["NeedDbInit"]); if (needDbInit) { var db = services.GetServices <SqlSugarClient>().FirstOrDefault(a => a.CurrentConnectionConfig.ConfigId == CCDBConfig.MainDbKey); // ISqlSugarClient sqlsugarClient = services.GetRequiredService<SqlSugarClient>(); DbSeed.InitDb(db); } } }