diff --git a/Src/Asp.Net/SqlServerTest/Demo/DemoH_Tenant.cs b/Src/Asp.Net/SqlServerTest/Demo/DemoH_Tenant.cs index 4ce9745d2..f71cbeafe 100644 --- a/Src/Asp.Net/SqlServerTest/Demo/DemoH_Tenant.cs +++ b/Src/Asp.Net/SqlServerTest/Demo/DemoH_Tenant.cs @@ -21,18 +21,19 @@ namespace OrmTest { public void Test() { - base.AsTenant().BeginTran(); + base.db.BeginTran(); base.GetList(); //调用内部仓储方法 base.ChangeRepository>().GetList();//调用外部仓储 - base.AsTenant().CommitTran(); + base.db.CommitTran(); } } public class Repository : SimpleClient where T : class, new() { + public SqlSugarClient db; public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null { if (context == null) @@ -53,10 +54,10 @@ namespace OrmTest ConnectionString = Config.ConnectionString2 } }); - base.Context = db; + var configId = typeof(T).GetCustomAttribute().configId; - db.ChangeDatabase(configId); - + Context = db.GetConnection(configId); + this.db = db; } } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoH_Tenant.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoH_Tenant.cs new file mode 100644 index 000000000..f71cbeafe --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoH_Tenant.cs @@ -0,0 +1,87 @@ +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 ####"); + new C1Service().Test(); + Console.WriteLine("#### SimpleClient End ####"); + } + public class C1Service : Repository + { + public void Test() + { + base.db.BeginTran(); + + base.GetList(); //调用内部仓储方法 + base.ChangeRepository>().GetList();//调用外部仓储 + + base.db.CommitTran(); + } + } + + + public class Repository : SimpleClient where T : class, new() + { + public SqlSugarClient db; + 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, + IsAutoCloseConnection = true, + ConnectionString = Config.ConnectionString + }, + new ConnectionConfig() + { + ConfigId="2", + DbType = SqlSugar.DbType.SqlServer, + IsAutoCloseConnection = true, + ConnectionString = Config.ConnectionString2 + } + }); + + var configId = typeof(T).GetCustomAttribute().configId; + Context = db.GetConnection(configId); + this.db = db; + } + } + + /// + /// 扩展方法,自带方法不能满足的时候可以添加新方法 + /// + /// + 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.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoJ_Report.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoJ_Report.cs new file mode 100644 index 000000000..f8e98922e --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoJ_Report.cs @@ -0,0 +1,142 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class DemoJ_Report + { + public static void Init() + { + Console.WriteLine(""); + Console.WriteLine("#### Utilities Start ####"); + + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.SqlServer, + ConnectionString = Config.ConnectionString, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + AopEvents = new AopEvents + { + OnLogExecuting = (sql, p) => + { + Console.WriteLine(sql); + Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value))); + } + } + }); + Demo1(db); + Demo2(db); + Demo3(db); + } + + private static void Demo1(SqlSugarClient db) + { + var list = new List() { 1, 2, 3 }; + var query1 = db.Queryable(); + var queryable2 = db.Reportable(list).ToQueryable(); + var x = db.Queryable(query1, queryable2, (x1, x2) => x1.Id.Equals(x2.ColumnName)) + .Select((x1, x2) => new { x = x1.Id, x2 = x2.ColumnName }).ToList(); + } + private static void Demo2(SqlSugarClient db) + { + var list = db.Queryable().ToList(); + var query1 = db.Queryable(); + var queryable2 = db.Reportable(list).ToQueryable(); + var x = db.Queryable(query1, queryable2, (x1, x2) => x1.Id.Equals(x2.OrderId)) + .Select((x1, x2) => new { name = x1.Name,id=x1.Id, orderid = x2.OrderId }).ToList(); + } + private static void Demo3(SqlSugarClient db) + { + db.CodeFirst.InitTables(); + db.Deleteable().ExecuteCommand(); + db.Insertable(new operateinfo() + { + id=1, + operate_type=1, + operate_time=Convert.ToDateTime("2021-1-1") + }).ExecuteCommand(); + db.Insertable(new operateinfo() + { + id = 1, + operate_type = 1, + operate_time = Convert.ToDateTime("2021-1-2") + }).ExecuteCommand(); + db.Insertable(new operateinfo() + { + id = 1, + operate_type = 1, + operate_time = Convert.ToDateTime("2021-3-1") + }).ExecuteCommand(); + db.Insertable(new operateinfo() + { + id = 1, + operate_type = 1, + operate_time = Convert.ToDateTime("2021-3-2") + }).ExecuteCommand(); + db.Insertable(new operateinfo() + { + id = 1, + operate_type = 1, + operate_time = Convert.ToDateTime("2021-4-2") + }).ExecuteCommand(); + + + var queryableLeft = db.Reportable(ReportableDateType.MonthsInLast1years).ToQueryable(); + var queryableRight = db.Queryable(); + var list= db.Queryable(queryableLeft, queryableRight, JoinType.Left, + (x1, x2) => x2.operate_time.ToString("yyyy-MM")== x1.ColumnName .ToString("yyyy-MM")) + .GroupBy((x1,x2)=>x1.ColumnName) + .Where(x1=>SqlFunc.Between(x1.ColumnName,"2021-01-01",DateTime.Now)) + .Select((x1, x2) => new + { + count=SqlFunc.AggregateSum(SqlFunc.IIF(x2.id>0,1,0)) , + date=x1.ColumnName.ToString("yyyy-MM") + + }).ToList(); + } + + + public partial class operateinfo + { + public operateinfo() + { + + + } + /// + /// Desc:操作序号 + /// Default: + /// Nullable:False + /// + public int id { get; set; } + + /// + /// Desc:操作时间 + /// Default: + /// Nullable:False + /// + public DateTime operate_time { get; set; } + + /// + /// Desc:操作类型 + /// Default: + /// Nullable:False + /// + public int operate_type { get; set; } + + /// + /// Desc:操作人编号 + /// Default: + /// Nullable:False + /// + public int user_id { get; set; } + + } + } + +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs index 182b9aa95..5afe2f00c 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs @@ -31,7 +31,7 @@ namespace OrmTest DemoE_CodeFirst.Init(); DemoF_Utilities.Init(); DemoG_SimpleClient.Init(); - + DemoJ_Report.Init(); //Unit test //NewUnitTest.Init();