2022-04-10 01:27:47 +08:00
|
|
|
|
using OrmTest.UnitTest.Models;
|
|
|
|
|
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;
|
|
|
|
|
|
2022-04-11 14:04:22 +08:00
|
|
|
|
db.CodeFirst.InitTables<StudentA, RoomA, SchoolA, TeacherA>();
|
2022-04-10 01:27:47 +08:00
|
|
|
|
db.DbMaintenance.TruncateTable<StudentA>();
|
|
|
|
|
db.DbMaintenance.TruncateTable<RoomA>();
|
|
|
|
|
db.DbMaintenance.TruncateTable<SchoolA>();
|
2022-04-10 11:03:55 +08:00
|
|
|
|
db.DbMaintenance.TruncateTable<TeacherA>();
|
2022-04-10 01:27:47 +08:00
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
2022-04-10 01:33:00 +08:00
|
|
|
|
db.Insertable(new SchoolA() { SchoolId = 1, SchoolName = "北大" }).ExecuteCommand();
|
2022-04-10 01:27:47 +08:00
|
|
|
|
db.Insertable(new SchoolA() { SchoolId = 2, SchoolName = "清华" }).ExecuteCommand();
|
|
|
|
|
|
2022-04-10 01:33:00 +08:00
|
|
|
|
db.Insertable(new StudentA() { StudentId = 1, SchoolId = 1, Name = "北大jack" }).ExecuteCommand();
|
2022-04-10 01:27:47 +08:00
|
|
|
|
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();
|
|
|
|
|
|
2022-04-11 14:04:22 +08:00
|
|
|
|
db.Insertable(new TeacherA() { SchoolId = 1, Id = 1, Name = "北大老师01" }).ExecuteCommand();
|
|
|
|
|
db.Insertable(new TeacherA() { SchoolId = 1, Id = 2, Name = "北大老师02" }).ExecuteCommand();
|
2022-04-10 11:03:55 +08:00
|
|
|
|
|
|
|
|
|
db.Insertable(new TeacherA() { SchoolId = 2, Id = 3, Name = "清华老师01" }).ExecuteCommand();
|
|
|
|
|
db.Insertable(new TeacherA() { SchoolId = 2, Id = 4, Name = "清华老师02" }).ExecuteCommand();
|
|
|
|
|
|
2022-04-10 01:33:00 +08:00
|
|
|
|
//先用Mapper导航映射查出第二层
|
|
|
|
|
var list = db.Queryable<StudentA>().Mapper(x => x.SchoolA, x => x.SchoolId).ToList();
|
2022-04-10 01:27:47 +08:00
|
|
|
|
|
2022-04-10 01:33:00 +08:00
|
|
|
|
//参数1 :将第二层对象合并成一个集合 参数2:委托
|
|
|
|
|
//说明:如果2级对象是集合用SelectMany
|
2022-04-10 01:27:47 +08:00
|
|
|
|
db.ThenMapper(list.Select(it => it.SchoolA), sch =>
|
|
|
|
|
{
|
2022-04-10 01:33:00 +08:00
|
|
|
|
//参数1: room表关联字段 参数2: school表关联字段, 参数3: school当前记录
|
|
|
|
|
sch.RoomList = db.Queryable<RoomA>().SetContext(room => room.SchoolId, () => sch.SchoolId, sch).ToList();
|
2022-04-10 11:03:55 +08:00
|
|
|
|
|
|
|
|
|
sch.TeacherList = db.Queryable<TeacherA>().SetContext(teachera => teachera.SchoolId, () => sch.SchoolId, sch).ToList();
|
2022-04-10 01:27:47 +08:00
|
|
|
|
});
|
2022-04-11 14:04:22 +08:00
|
|
|
|
|
|
|
|
|
db.Queryable<StudentA>()
|
|
|
|
|
.Includes(x => x.SchoolA, x => x.RoomList)//2个参数就是 then Include
|
|
|
|
|
.Includes(x => x.Books)
|
|
|
|
|
.ToList();
|
2022-04-10 01:27:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public class StudentA
|
|
|
|
|
{
|
|
|
|
|
[SugarColumn(IsPrimaryKey = true)]
|
|
|
|
|
public int StudentId { get; set; }
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public int SchoolId { get; set; }
|
2022-04-11 14:04:22 +08:00
|
|
|
|
[Navigat(nameof(SchoolId))]
|
2022-04-10 01:27:47 +08:00
|
|
|
|
public SchoolA SchoolA { get; set; }
|
2022-04-11 14:04:22 +08:00
|
|
|
|
[Navigat(nameof(BookA.studenId))]
|
|
|
|
|
public List<BookA> Books { get; set; }
|
2022-04-10 01:27:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SchoolA
|
|
|
|
|
{
|
|
|
|
|
[SugarColumn(IsPrimaryKey = true)]
|
|
|
|
|
public int SchoolId { get; set; }
|
|
|
|
|
public string SchoolName { get; set; }
|
|
|
|
|
[SugarColumn(IsIgnore = true)]
|
|
|
|
|
public List<RoomA> RoomList { get; set; }
|
2022-04-10 11:03:55 +08:00
|
|
|
|
[SugarColumn(IsIgnore = true)]
|
|
|
|
|
public List<TeacherA> TeacherList { get; set; }
|
|
|
|
|
}
|
|
|
|
|
public class TeacherA
|
|
|
|
|
{
|
|
|
|
|
[SugarColumn(IsPrimaryKey = true)]
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
public int SchoolId { get; set; }
|
|
|
|
|
public string Name { get; set; }
|
2022-04-10 01:27:47 +08:00
|
|
|
|
}
|
|
|
|
|
public class RoomA
|
|
|
|
|
{
|
|
|
|
|
[SugarColumn(IsPrimaryKey = true)]
|
|
|
|
|
public int RoomId { get; set; }
|
|
|
|
|
public string RoomName { get; set; }
|
|
|
|
|
public int SchoolId { get; set; }
|
|
|
|
|
}
|
2022-04-11 14:04:22 +08:00
|
|
|
|
public class BookA
|
|
|
|
|
{
|
|
|
|
|
public int studenId { get; set; }
|
|
|
|
|
}
|
2022-04-10 01:27:47 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|