diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs index e82eb7912..daa6bb210 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs @@ -31,6 +31,7 @@ namespace OrmTest } public static void Init() { + UCustom012.Init(); UCustom01.Init(); UCustom02.Init(); UCustom03.Init(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UCustom012.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UCustom012.cs new file mode 100644 index 000000000..221f8b7d0 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UCustom012.cs @@ -0,0 +1,80 @@ + +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class UCustom012 + { + + public static void Init() + { + var db = NewUnitTest.Db; + + db.CodeFirst.InitTables(); + 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(); + db.Insertable(new RoomA() { RoomId = 4, RoomName = "清华001厅", SchoolId = 2 }).ExecuteCommand(); + db.Insertable(new RoomA() { RoomId = 5, RoomName = "清华002厅", SchoolId = 2 }).ExecuteCommand(); + db.Insertable(new RoomA() { RoomId = 6, RoomName = "清华003厅", SchoolId = 2 }).ExecuteCommand(); + + + db.Insertable(new SchoolA() { SchoolId = 1, SchoolName = "北大" }).ExecuteCommand(); + db.Insertable(new SchoolA() { SchoolId = 2, SchoolName = "清华" }).ExecuteCommand(); + + db.Insertable(new StudentA() { StudentId = 1, SchoolId = 1, Name = "北大jack" }).ExecuteCommand(); + db.Insertable(new StudentA() { StudentId = 2, SchoolId = 1, Name = "北大tom" }).ExecuteCommand(); + db.Insertable(new StudentA() { StudentId = 3, SchoolId = 2, Name = "清华jack" }).ExecuteCommand(); + db.Insertable(new StudentA() { StudentId = 4, SchoolId = 2, Name = "清华tom" }).ExecuteCommand(); + + //先用Mapper导航映射查出第二层 + var list = db.Queryable().Mapper(x => x.SchoolA, x => x.SchoolId).ToList(); + + //参数1 :将第二层对象合并成一个集合 参数2:委托 + //说明:如果2级对象是集合用SelectMany + db.ThenMapper(list.Select(it => it.SchoolA), sch => + { + //参数1: room表关联字段 参数2: school表关联字段, 参数3: school当前记录 + sch.RoomList = db.Queryable().SetContext(room => room.SchoolId, () => sch.SchoolId, sch).ToList(); + }); + + + } + public class StudentA + { + [SugarColumn(IsPrimaryKey = true)] + public int StudentId { get; set; } + public string Name { get; set; } + public int SchoolId { get; set; } + [SugarColumn(IsIgnore = true)] + public SchoolA SchoolA { get; set; } + } + + public class SchoolA + { + [SugarColumn(IsPrimaryKey = true)] + public int SchoolId { get; set; } + public string SchoolName { get; set; } + [SugarColumn(IsIgnore = true)] + public List RoomList { get; set; } + } + + public class RoomA + { + [SugarColumn(IsPrimaryKey = true)] + public int RoomId { get; set; } + public string RoomName { get; set; } + public int SchoolId { get; set; } + } + + } +}