From 7bd5b3fd6020475b4a5e994dd9d20f43c56a055e Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 12 Apr 2022 08:49:37 +0800 Subject: [PATCH] Update unit test --- .../SqlSeverTest/UnitTest/UCustom012.cs | 114 +++++++++++++++++- .../SqlSugar/SqlSugarForCore.nuspec | 2 +- 2 files changed, 109 insertions(+), 7 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UCustom012.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UCustom012.cs index 221f8b7d0..b57b29f12 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UCustom012.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UCustom012.cs @@ -15,11 +15,13 @@ namespace OrmTest { var db = NewUnitTest.Db; - db.CodeFirst.InitTables(); + db.CodeFirst.InitTables(); + db.CodeFirst.InitTables(); db.DbMaintenance.TruncateTable(); db.DbMaintenance.TruncateTable(); db.DbMaintenance.TruncateTable(); - + db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); db.Insertable(new RoomA() { RoomId = 1, RoomName = "北大001室", SchoolId = 1 }).ExecuteCommand(); db.Insertable(new RoomA() { RoomId = 2, RoomName = "北大002室", SchoolId = 1 }).ExecuteCommand(); db.Insertable(new RoomA() { RoomId = 3, RoomName = "北大003室", SchoolId = 1 }).ExecuteCommand(); @@ -36,6 +38,19 @@ namespace OrmTest db.Insertable(new StudentA() { StudentId = 3, SchoolId = 2, Name = "清华jack" }).ExecuteCommand(); db.Insertable(new StudentA() { StudentId = 4, SchoolId = 2, Name = "清华tom" }).ExecuteCommand(); + db.Insertable(new TeacherA() { SchoolId = 1, Id = 1, Name = "北大老师01" }).ExecuteCommand(); + db.Insertable(new TeacherA() { SchoolId = 1, Id = 2, Name = "北大老师02" }).ExecuteCommand(); + + db.Insertable(new TeacherA() { SchoolId = 2, Id = 3, Name = "清华老师01" }).ExecuteCommand(); + db.Insertable(new TeacherA() { SchoolId = 2, Id = 4, Name = "清华老师02" }).ExecuteCommand(); + + + db.Insertable(new BookA() { BookId = 1, Name = "java", studenId = 1 }).ExecuteCommand(); + db.Insertable(new BookA() { BookId = 2, Name = "c#2", studenId = 2 }).ExecuteCommand(); + db.Insertable(new BookA() { BookId = 3, Name = "c#1", studenId = 2 }).ExecuteCommand(); + db.Insertable(new BookA() { BookId = 4, Name = "php", studenId = 3 }).ExecuteCommand(); + db.Insertable(new BookA() { BookId = 5, Name = "js", studenId = 4 }).ExecuteCommand(); + //先用Mapper导航映射查出第二层 var list = db.Queryable().Mapper(x => x.SchoolA, x => x.SchoolId).ToList(); @@ -45,29 +60,108 @@ namespace OrmTest { //参数1: room表关联字段 参数2: school表关联字段, 参数3: school当前记录 sch.RoomList = db.Queryable().SetContext(room => room.SchoolId, () => sch.SchoolId, sch).ToList(); + + sch.TeacherList = db.Queryable().SetContext(teachera => teachera.SchoolId, () => sch.SchoolId, sch).ToList(); }); + var list2 = db.Queryable() + .Includes(x => x.SchoolA, x => x.RoomList)//2个参数就是 then Include + .Includes(x => x.Books) + .ToList(); + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); + db.Insertable(new A1() { Id = 1, Name = "a1" }).ExecuteCommand(); + db.Insertable(new A1() { Id = 2, Name = "a2" }).ExecuteCommand(); + db.Insertable(new B1() { Id = 1, Name = "b1" }).ExecuteCommand(); + db.Insertable(new B1() { Id = 2, Name = "b2" }).ExecuteCommand(); + db.Insertable(new ABMapping1() { AId = 1, BId = 1 }).ExecuteCommand(); + db.Insertable(new ABMapping1() { AId = 2, BId = 1 }).ExecuteCommand(); + db.Insertable(new ABMapping1() { AId = 2, BId = 2 }).ExecuteCommand(); + var list3 = db.Queryable().Includes(x => x.BList).ToList(); + var list31 = db.Queryable().Includes(x => x.BList, x => x.AList).ToList(); + + db.CodeFirst.InitTables(typeof(Tree1)); + db.DbMaintenance.TruncateTable("Tree1"); + db.Insertable(new Tree1() { Id = 1, Name = "01" }).ExecuteCommand(); + db.Insertable(new Tree1() { Id = 2, Name = "0101", ParentId = 1 }).ExecuteCommand(); + db.Insertable(new Tree1() { Id = 3, Name = "0102", ParentId = 1 }).ExecuteCommand(); + db.Insertable(new Tree1() { Id = 4, Name = "02" }).ExecuteCommand(); + db.Insertable(new Tree1() { Id = 5, Name = "0201", ParentId = 2 }).ExecuteCommand(); + db.Insertable(new Tree1() { Id = 6, Name = "020101", ParentId = 5 }).ExecuteCommand(); + var list4 = db.Queryable() + .Includes(it => it.Child, it => it.Child, it => it.Child) + .Includes(it => it.Parent, it => it.Parent, it => it.Parent, it => it.Parent) + .ToList(); + //var json = db.Utilities.SerializeObject(list4); } + + public class Tree1 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public int Id { get; set; } + public string Name { get; set; } + public int ParentId { get; set; } + [Navigat(NavigatType.OneToOne, nameof(ParentId))] + public Tree1 Parent { get; set; } + [Navigat(NavigatType.OneToMany, nameof(Tree1.ParentId))] + public List Child { get; set; } + } + public class ABMapping1 + { + [SugarColumn(IsPrimaryKey = true)] + public int AId { get; set; } + [SugarColumn(IsPrimaryKey = true)] + public int BId { get; set; } + } + public class A1 + { + [SugarColumn(IsPrimaryKey = true)] + public int Id { get; set; } + public string Name { get; set; } + [Navigat(typeof(ABMapping1), nameof(ABMapping1.AId), nameof(ABMapping1.BId))] + public List BList { get; set; } + } + public class B1 + { + [SugarColumn(IsPrimaryKey = true)] + public int Id { get; set; } + public string Name { get; set; } + [Navigat(typeof(ABMapping1), nameof(ABMapping1.BId), nameof(ABMapping1.AId))] + public List AList { get; set; } + } + public class StudentA { [SugarColumn(IsPrimaryKey = true)] public int StudentId { get; set; } public string Name { get; set; } public int SchoolId { get; set; } - [SugarColumn(IsIgnore = true)] + [Navigat(NavigatType.OneToOne, nameof(SchoolId))] public SchoolA SchoolA { get; set; } - } + [Navigat(NavigatType.OneToMany, nameof(BookA.studenId))] + public List Books { get; set; } + } public class SchoolA { [SugarColumn(IsPrimaryKey = true)] public int SchoolId { get; set; } public string SchoolName { get; set; } - [SugarColumn(IsIgnore = true)] + [Navigat(NavigatType.OneToMany, nameof(RoomA.SchoolId))] public List RoomList { get; set; } + [SugarColumn(IsIgnore = true)] + public List TeacherList { get; set; } + } + public class TeacherA + { + [SugarColumn(IsPrimaryKey = true)] + public int Id { get; set; } + public int SchoolId { get; set; } + public string Name { get; set; } } - public class RoomA { [SugarColumn(IsPrimaryKey = true)] @@ -75,6 +169,14 @@ namespace OrmTest public string RoomName { get; set; } public int SchoolId { get; set; } } + public class BookA + { + [SugarColumn(IsPrimaryKey = true)] + public int BookId { get; set; } + + public string Name { get; set; } + public int studenId { get; set; } + } } } diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugarForCore.nuspec b/Src/Asp.NetCore2/SqlSugar/SqlSugarForCore.nuspec index 87660f624..84f5e8c9a 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugarForCore.nuspec +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugarForCore.nuspec @@ -2,7 +2,7 @@ SqlSugarCore - 5.0.6.7 + 5.0.6.8-preview01 sunkaixuan 果糖大数据科技 http://www.apache.org/licenses/LICENSE-2.0.html