From b6eb16da485fdd1a5f66442a3a68c200e2216fe6 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 11 Nov 2023 02:40:10 +0800 Subject: [PATCH] Add split table demo --- Src/Asp.Net/SqlServerTest/Program.cs | 2 +- .../SqlServerTest/SqlServerTest.csproj | 1 + Src/Asp.Net/SqlServerTest/a4_SplitTable.cs | 61 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 Src/Asp.Net/SqlServerTest/a4_SplitTable.cs diff --git a/Src/Asp.Net/SqlServerTest/Program.cs b/Src/Asp.Net/SqlServerTest/Program.cs index 9a91bbf75..6ec985830 100644 --- a/Src/Asp.Net/SqlServerTest/Program.cs +++ b/Src/Asp.Net/SqlServerTest/Program.cs @@ -10,7 +10,6 @@ namespace OrmTest { //Each example will automatically create a table and can run independently. //每个例子都会自动建表 并且可以独立运行 - _1_CodeFirst.Init(); _2_DbFirst.Init(); _3_EasyQuery.Init(); @@ -23,6 +22,7 @@ namespace OrmTest _a1_Delete.Init(); _a2_Sql.Init(); _a3_Merge.Init(); + _a4_SplitTable.Init(); } } diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index d690febf3..da7612601 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -72,6 +72,7 @@ + diff --git a/Src/Asp.Net/SqlServerTest/a4_SplitTable.cs b/Src/Asp.Net/SqlServerTest/a4_SplitTable.cs new file mode 100644 index 000000000..154401b67 --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/a4_SplitTable.cs @@ -0,0 +1,61 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _a4_SplitTable + { + // Method for initialization and testing of split tables + // 用于初始化和测试分表的方法 + public static void Init() + { + // Obtain a new instance of SqlSugarClient + // 获取 SqlSugarClient 的新实例 + SqlSugarClient db = DbHelper.GetNewDb(); + + + // Entities change the synchronization table structure + // 实体变化同步表结构 + db.CodeFirst.SplitTables().InitTables(); + + // Insert records into the split table and create table + // 向分表插入记录并创建表 + db.Insertable(new SplitTableDemo() { Name = "jack", Time = DateTime.Now }).SplitTable().ExecuteCommand(); + db.Insertable(new SplitTableDemo() { Name = "jack2", Time = DateTime.Now.AddDays(-11) }).SplitTable().ExecuteCommand(); + + // Query records from the split table within a specified date range + // 在指定日期范围内从分表查询记录 + var list = db.Queryable() + .SplitTable(DateTime.Now.Date.AddYears(-1), DateTime.Now) + .ToList(); + + // Update records from the split table + // 从分表更新记录 + var updateList = list.Take(2).ToList(); + db.Updateable(updateList).SplitTable().ExecuteCommand(); + } + + // Entity class representing the split table + // 代表分表的实体类 + [SplitTable(SplitType.Day)] // Specify the split type as "Day" + // 指定分表类型为“Day” + [SqlSugar.SugarTable("SplitTableDemo_{year}{month}{day}")] // Specify the table name pattern + // 指定表名模式 + public class SplitTableDemo + { + [SugarColumn(IsPrimaryKey = true)] // Specify primary key + // 指定主键 + public Guid Pk { get; set; } + public string Name { get; set; } + + [SugarColumn(IsNullable = true)] + [SplitField] // Mark the field as a split field + // 将字段标记为分表字段 + public DateTime Time { get; set; } + } + } +} \ No newline at end of file