SqlSugarRepository.cs 841 B

1234567891011121314151617181920212223242526
  1. namespace Admin.NET.Core;
  2. /// <summary>
  3. /// SqlSugar仓储类
  4. /// </summary>
  5. /// <typeparam name="T"></typeparam>
  6. public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
  7. {
  8. protected ITenant iTenant = null; // 多租户事务
  9. public SqlSugarRepository(ISqlSugarClient context = null) : base(context)
  10. {
  11. iTenant = App.GetRequiredService<ISqlSugarClient>().AsTenant();
  12. // 根据租户业务实体是否切库
  13. if (typeof(T).IsDefined(typeof(TenantBusinessAttribute), false))
  14. {
  15. var tenantId = App.GetRequiredService<UserManager>().TenantId; // 根据租户Id切库
  16. base.Context = SqlSugarSetup.InitTenantConnection(iTenant, tenantId);
  17. }
  18. else
  19. {
  20. base.Context = iTenant.GetConnectionScopeWithAttr<T>();
  21. }
  22. }
  23. }