From 11f0204af9ada778fe411678e7209cd89d131e0c Mon Sep 17 00:00:00 2001 From: sunkaixuna <610262374@qq.com> Date: Sat, 24 Apr 2021 17:20:15 +0800 Subject: [PATCH] Update demo --- .../SqlServerTest/Demo/DemoH_Tenant.cs | 81 +++++++++++++++++++ Src/Asp.Net/SqlServerTest/Program.cs | 4 +- .../SqlServerTest/SqlServerTest.csproj | 1 + .../Entities/Mapping/SugarMappingAttribute.cs | 11 +++ 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 Src/Asp.Net/SqlServerTest/Demo/DemoH_Tenant.cs diff --git a/Src/Asp.Net/SqlServerTest/Demo/DemoH_Tenant.cs b/Src/Asp.Net/SqlServerTest/Demo/DemoH_Tenant.cs new file mode 100644 index 000000000..93e4e87b7 --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/Demo/DemoH_Tenant.cs @@ -0,0 +1,81 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class DemoH_Tenant + { + public static void Init() + { + Console.WriteLine(""); + Console.WriteLine("#### SimpleClient Start ####"); + var rep1=new Repository(); + rep1.GetList(); + var rep2 = new Repository(); + rep2.GetList(); + Console.WriteLine("#### SimpleClient End ####"); + } + + public class Repository : SimpleClient where T : class, new() + { + public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null + { + if (context == null) + { + var db = new SqlSugarClient(new List { + new ConnectionConfig() + { + ConfigId=1, + DbType = SqlSugar.DbType.SqlServer, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + ConnectionString = Config.ConnectionString + }, + new ConnectionConfig() + { + ConfigId="2", + DbType = SqlSugar.DbType.SqlServer, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + ConnectionString = Config.ConnectionString2 + } + }); + var configId = typeof(T).GetCustomAttribute().configId; + base.Context = db.GetConnection(configId); + base.Context.CodeFirst.InitTables(); + db.Aop.OnLogExecuting = (s, p) => + { + Console.WriteLine(s); + }; + } + } + + /// + /// 扩展方法,自带方法不能满足的时候可以添加新方法 + /// + /// + public List CommQuery(string sql) + { + //base.Context.Queryable().ToList();可以拿到SqlSugarClient 做复杂操作 + return base.Context.Queryable().Where(sql).ToList(); + } + + } + + [TenantAttribute("1")] + public class C1Table + { + public string Id { get; set; } + } + [TenantAttribute("2")] + public class C2Table + { + public string Id { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.Net/SqlServerTest/Program.cs b/Src/Asp.Net/SqlServerTest/Program.cs index 7dfca151e..e740fff19 100644 --- a/Src/Asp.Net/SqlServerTest/Program.cs +++ b/Src/Asp.Net/SqlServerTest/Program.cs @@ -30,9 +30,9 @@ namespace OrmTest DemoE_CodeFirst.Init(); DemoF_Utilities.Init(); DemoG_SimpleClient.Init(); - + DemoH_Tenant.Init(); //Unit test - //NewUnitTest.Init(); + //NewUnitTest.Init(); //Rest Data NewUnitTest.RestData(); diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index 89c5bbf32..b8854ed10 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -67,6 +67,7 @@ + diff --git a/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs b/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs index ee85b110f..e73ed227c 100644 --- a/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs +++ b/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs @@ -192,4 +192,15 @@ namespace SqlSugar } + + [AttributeUsage(AttributeTargets.Class, Inherited = true)] + public class TenantAttribute :Attribute + { + public object configId { get; set; } + public TenantAttribute(object configId) + { + this.configId = configId; + } + } + }