// Admin.NET 项目的版æƒã€å•†æ ‡ã€ä¸“利和其他相关æƒåˆ©å‡å—ç›¸åº”æ³•å¾‹æ³•è§„çš„ä¿æŠ¤ã€‚ä½¿ç”¨æœ¬é¡¹ç›®åº”éµå®ˆç›¸å…³æ³•律法规和许å¯è¯çš„è¦æ±‚。 // // 本项目主è¦éµå¾ª MIT 许å¯è¯å’Œ Apache 许å¯è¯ï¼ˆç‰ˆæœ¬ 2.0)进行分å‘和使用。许å¯è¯ä½äºŽæºä»£ç æ ‘æ ¹ç›®å½•ä¸çš„ LICENSE-MIT å’Œ LICENSE-APACHE 文件。 // // ä¸å¾—利用本项目从事å±å®³å›½å®¶å®‰å…¨ã€æ‰°ä¹±ç¤¾ä¼šç§©åºã€ä¾µçŠ¯ä»–äººåˆæ³•æƒç›Šç‰æ³•å¾‹æ³•è§„ç¦æ¢çš„æ´»åЍï¼ä»»ä½•基于本项目二次开å‘è€Œäº§ç”Ÿçš„ä¸€åˆ‡æ³•å¾‹çº çº·å’Œè´£ä»»ï¼Œæˆ‘ä»¬ä¸æ‰¿æ‹…ä»»ä½•è´£ä»»ï¼ namespace Admin.NET.Core; /// <summary> /// SqlSugar 实体仓储 /// </summary> /// <typeparam name="T"></typeparam> public class SqlSugarRepository<T> : SimpleClient<T>, ISqlSugarRepository<T> where T : class, new() { public SqlSugarRepository() { var iTenant = SqlSugarSetup.ITenant; // App.GetRequiredService<ISqlSugarClient>().AsTenant(); base.Context = iTenant.GetConnectionScope(SqlSugarConst.MainConfigId); // 若实体贴有多库特性,则返回指定库连接 if (typeof(T).IsDefined(typeof(TenantAttribute), false)) { base.Context = iTenant.GetConnectionScopeWithAttr<T>(); return; } // 若实体贴有日志表特性,则返回日志库连接 if (typeof(T).IsDefined(typeof(LogTableAttribute), false)) { if (iTenant.IsAnyConnection(SqlSugarConst.LogConfigId)) base.Context = iTenant.GetConnectionScope(SqlSugarConst.LogConfigId); return; } // 若实体贴有系统表特性,则返回默认库连接 if (typeof(T).IsDefined(typeof(SysTableAttribute), false)) return; // è‹¥æœªè´´ä»»ä½•è¡¨ç‰¹æ€§æˆ–å½“å‰æœªç™»å½•或是默认租户Id,则返回默认库连接 var tenantId = App.User?.FindFirst(ClaimConst.TenantId)?.Value; if (string.IsNullOrWhiteSpace(tenantId) || tenantId == SqlSugarConst.MainConfigId) return; // æ ¹æ®ç§Ÿæˆ·Id切æ¢åº“连接 为空则返回默认库连接 var sqlSugarScopeProviderTenant = App.GetRequiredService<SysTenantService>().GetTenantDbConnectionScope(long.Parse(tenantId)); if (sqlSugarScopeProviderTenant == null) return; base.Context = sqlSugarScopeProviderTenant; } #region 分表æ“作 public async Task<bool> SplitTableInsertAsync(T input) { return await base.AsInsertable(input).SplitTable().ExecuteCommandAsync() > 0; } public async Task<bool> SplitTableInsertAsync(List<T> input) { return await base.AsInsertable(input).SplitTable().ExecuteCommandAsync() > 0; } public async Task<bool> SplitTableUpdateAsync(T input) { return await base.AsUpdateable(input).SplitTable().ExecuteCommandAsync() > 0; } public async Task<bool> SplitTableUpdateAsync(List<T> input) { return await base.AsUpdateable(input).SplitTable().ExecuteCommandAsync() > 0; } public async Task<bool> SplitTableDeleteableAsync(T input) { return await base.Context.Deleteable(input).SplitTable().ExecuteCommandAsync() > 0; } public async Task<bool> SplitTableDeleteableAsync(List<T> input) { return await base.Context.Deleteable(input).SplitTable().ExecuteCommandAsync() > 0; } public Task<T> SplitTableGetFirstAsync(Expression<Func<T, bool>> whereExpression) { return base.AsQueryable().SplitTable().FirstAsync(whereExpression); } public Task<bool> SplitTableIsAnyAsync(Expression<Func<T, bool>> whereExpression) { return base.Context.Queryable<T>().Where(whereExpression).SplitTable().AnyAsync(); } public Task<List<T>> SplitTableGetListAsync() { return Context.Queryable<T>().SplitTable().ToListAsync(); } public Task<List<T>> SplitTableGetListAsync(Expression<Func<T, bool>> whereExpression) { return Context.Queryable<T>().Where(whereExpression).SplitTable().ToListAsync(); } public Task<List<T>> SplitTableGetListAsync(Expression<Func<T, bool>> whereExpression, string[] tableNames) { return Context.Queryable<T>().Where(whereExpression).SplitTable(t => t.InTableNames(tableNames)).ToListAsync(); } #endregion 分表æ“作 }