diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index 986d0fcfc..6edee663c 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs @@ -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(); + 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(); diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 8c1e9cc5d..397826948 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -327,6 +327,12 @@ namespace SqlSugar var entityName = typeof(T).Name; return _As(tableName, entityName); } + public ISugarQueryable AsWithAttr() + { + var asName=GetTableName(this.EntityInfo, this.EntityInfo.DbTableName); + this.QueryBuilder.IsCrossQueryWithAttr = true; + return this.AS(asName); + } public ISugarQueryable AsType(Type tableNameType) { return AS(this.Context.EntityMaintenance.GetEntityInfo(tableNameType).DbTableName); diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/ICodeFirst.cs b/Src/Asp.NetCore2/SqlSugar/Interface/ICodeFirst.cs index f62d070bc..e342f1110 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/ICodeFirst.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/ICodeFirst.cs @@ -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(); void InitTables(); diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs b/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs index 66b0fbe71..8c01a4cea 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs @@ -19,6 +19,7 @@ namespace SqlSugar ISugarQueryable Clone(); ISugarQueryable AS(string tableName); ISugarQueryable AS(string tableName); + ISugarQueryable AsWithAttr(); ISugarQueryable AsType(Type tableNameType); ISugarQueryable With(string withString); //ISugarQueryable CrossQueryWithAttr(); diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs b/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs index 80580544e..f354422fb 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs @@ -875,6 +875,14 @@ namespace SqlSugar var configId = attr.configId; return this.GetConnection(configId); } + public SqlSugarProvider GetConnectionWithAttr(Type type) + { + var attr = type.GetCustomAttribute(); + if (attr == null) + return this.GetConnection(this.CurrentConnectionConfig.ConfigId); + var configId = attr.configId; + return this.GetConnection(configId); + } public SqlSugarScopeProvider GetConnectionScopeWithAttr() { var attr = typeof(T).GetCustomAttribute();