1. Queryable.AsWithAttr()
2. CodeFirst.InitTableWithAttr()
This commit is contained in:
sunkaixuan 2023-07-19 13:02:25 +08:00
parent c896b1da5a
commit 34b9a94ba8
5 changed files with 32 additions and 1 deletions

View File

@ -45,7 +45,22 @@ namespace SqlSugar
DefultLength = length;
return this;
}
public void InitTablesWithAttr(params Type[] entityTypes)
{
foreach (var item in entityTypes)
{
var attr = item.GetCustomAttribute<TenantAttribute>();
if (attr==null||this.Context?.Root == null)
{
this.Context.CodeFirst.InitTables(item);
}
else
{
var newDb = this.Context.Root.GetConnectionWithAttr(item);
newDb.CodeFirst.InitTables(item);
}
}
}
public virtual void InitTables(Type entityType)
{
var splitTableAttribute = entityType.GetCustomAttribute<SplitTableAttribute>();

View File

@ -327,6 +327,12 @@ namespace SqlSugar
var entityName = typeof(T).Name;
return _As(tableName, entityName);
}
public ISugarQueryable<T> AsWithAttr()
{
var asName=GetTableName(this.EntityInfo, this.EntityInfo.DbTableName);
this.QueryBuilder.IsCrossQueryWithAttr = true;
return this.AS(asName);
}
public ISugarQueryable<T> AsType(Type tableNameType)
{
return AS(this.Context.EntityMaintenance.GetEntityInfo(tableNameType).DbTableName);

View File

@ -16,6 +16,7 @@ namespace SqlSugar
void InitTables(string entitiesNamespace);
void InitTables(string[] entitiesNamespaces);
void InitTables(params Type[] entityTypes);
void InitTablesWithAttr(params Type[] entityTypes);
void InitTables(Type entityType);
void InitTables<T>();
void InitTables<T, T2>();

View File

@ -19,6 +19,7 @@ namespace SqlSugar
ISugarQueryable<T> Clone();
ISugarQueryable<T> AS<T2>(string tableName);
ISugarQueryable<T> AS(string tableName);
ISugarQueryable<T> AsWithAttr();
ISugarQueryable<T> AsType(Type tableNameType);
ISugarQueryable<T> With(string withString);
//ISugarQueryable<T> CrossQueryWithAttr();

View File

@ -875,6 +875,14 @@ namespace SqlSugar
var configId = attr.configId;
return this.GetConnection(configId);
}
public SqlSugarProvider GetConnectionWithAttr(Type type)
{
var attr = type.GetCustomAttribute<TenantAttribute>();
if (attr == null)
return this.GetConnection(this.CurrentConnectionConfig.ConfigId);
var configId = attr.configId;
return this.GetConnection(configId);
}
public SqlSugarScopeProvider GetConnectionScopeWithAttr<T>()
{
var attr = typeof(T).GetCustomAttribute<TenantAttribute>();