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) { ISqlSugarClient db = services.GetRequiredService <SqlSugarClient>(); DbSeed.InitTables(db); } } }