From 34b9a94ba8bffcedee9a064b56356d81ef3f917c Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 19 Jul 2023 13:02:25 +0800 Subject: [PATCH] Add: 1. Queryable.AsWithAttr() 2. CodeFirst.InitTableWithAttr() --- .../CodeFirstProvider/CodeFirstProvider.cs | 17 ++++++++++++++++- .../QueryableProvider/QueryableProvider.cs | 6 ++++++ Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs | 1 + Src/Asp.Net/SqlSugar/Interface/IQueryable.cs | 1 + Src/Asp.Net/SqlSugar/SqlSugarClient.cs | 8 ++++++++ 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index 986d0fcfc..6edee663c 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.Net/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.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 8c1e9cc5d..397826948 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/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.Net/SqlSugar/Interface/ICodeFirst.cs b/Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs index f62d070bc..e342f1110 100644 --- a/Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs +++ b/Src/Asp.Net/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.Net/SqlSugar/Interface/IQueryable.cs b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs index 66b0fbe71..8c01a4cea 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.Net/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.Net/SqlSugar/SqlSugarClient.cs b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs index 80580544e..f354422fb 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.Net/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();