瀏覽代碼

😎优化仓储基类

zuohuaijun 2 年之前
父節點
當前提交
bb512df607

+ 1 - 1
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarRepository.cs

@@ -12,7 +12,7 @@ public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
 {
     public SqlSugarRepository()
     {
-        var iTenant = App.GetRequiredService<ISqlSugarClient>().AsTenant();
+        var iTenant = SqlSugarSetup.ITenant; // App.GetRequiredService<ISqlSugarClient>().AsTenant();
         base.Context = iTenant.GetConnectionScope(SqlSugarConst.MainConfigId);
 
         // 若实体贴有多库特性,则返回指定库连接

+ 4 - 0
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -6,6 +6,9 @@ namespace Admin.NET.Core;
 
 public static class SqlSugarSetup
 {
+    // 多租户实例
+    public static ITenant ITenant { get; set; }
+
     /// <summary>
     /// SqlSugar 上下文初始化
     /// </summary>
@@ -39,6 +42,7 @@ public static class SqlSugarSetup
                 SetDbDiffLog(dbProvider, config);
             });
         });
+        ITenant = sqlSugar;
 
         services.AddSingleton<ISqlSugarClient>(sqlSugar); // 单例注册
         services.AddScoped(typeof(SqlSugarRepository<>)); // 仓储注册