Add db.GetConnectionWithAttr

This commit is contained in:
sunkaixuan 2022-04-09 12:09:40 +08:00
parent 7982e5aa03
commit 06d4f13599
4 changed files with 17 additions and 0 deletions

View File

@ -21,6 +21,7 @@ namespace SqlSugar
Task<DbResult<T>> UseTranAsync<T>(Func<Task<T>> action, Action<Exception> errorCallBack = null);
void AddConnection(ConnectionConfig connection);
SqlSugarProvider GetConnection(dynamic configId);
SqlSugarProvider GetConnectionWithAttr<T>();
bool IsAnyConnection(dynamic configId);
void Close();

View File

@ -5,6 +5,7 @@ using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@ -633,6 +634,13 @@ namespace SqlSugar
});
}
}
public SqlSugarProvider GetConnectionWithAttr<T>()
{
var attr = typeof(T).GetCustomAttribute<TenantAttribute>();
Check.ExceptionEasy("not TenantAttribute", "不存在特性 TenantAttribute");
var configId = attr.configId;
return this.GetConnection(configId);
}
public SqlSugarProvider GetConnection(dynamic configId)
{
InitTenant();

View File

@ -158,6 +158,10 @@ namespace SqlSugar
{
return ScopedContext.GetConnection(configId);
}
public SqlSugarProvider GetConnectionWithAttr<T>()
{
return ScopedContext.GetConnectionWithAttr<T>();
}
public DateTime GetDate()
{

View File

@ -29,5 +29,9 @@ namespace SqlSugar
if (isException)
throw new SqlSugarException(string.Format(message, args));
}
public static void ExceptionEasy(string enMessage, string cnMessage)
{
throw new SqlSugarException(ErrorMessage.GetThrowMessage(enMessage, cnMessage));
}
}
}