using AssetManager.Data.Repositories; using Microsoft.Extensions.DependencyInjection; using SqlSugar; namespace AssetManager.Data; public static class DatabaseExtensions { public static IServiceCollection AddDatabase(this IServiceCollection services) { // SqlSugarClient 超高性能模式:每次请求创建新实例 // Scoped 确保:同一 HTTP 请求内复用,不同请求隔离 services.AddScoped(s => { return SqlSugarConfig.GetSqlSugarClient(); }); services.AddScoped(); // Repository 层 services.AddScoped(); services.AddScoped(); return services; } public static void InitializeDatabase(this IServiceProvider serviceProvider) { using var scope = serviceProvider.CreateScope(); var dbService = scope.ServiceProvider.GetRequiredService(); dbService.InitializeDatabase(); } }