SqlSugar/Src/Asp.Net/SqlServerTest/UnitTest/UOneManyMany3.cs

167 lines
6.3 KiB
C#
Raw Normal View History

2022-07-03 03:18:39 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
2022-07-03 04:23:35 +08:00
public class UOneManyMany3
2022-07-03 03:18:39 +08:00
{
public static void init()
{
var db = NewUnitTest.Db;
2022-07-03 04:23:35 +08:00
db.CodeFirst.InitTables<Student_003, School_003, Room_003, Desk_003>();
db.DbMaintenance.TruncateTable<Student_003, School_003, Room_003, Desk_003>();
2022-07-03 03:18:39 +08:00
2022-07-03 04:23:35 +08:00
db.Insertable(new Student_003() { sid = 1, Name = "北大jack", SchoolId = 1 }).ExecuteCommand();
db.Insertable(new Student_003() { sid = 2, Name = "青华jack", SchoolId = 2 }).ExecuteCommand();
2022-07-03 03:18:39 +08:00
2022-07-03 04:23:35 +08:00
db.Insertable(new School_003() { scid = 1, schname = "北大" }).ExecuteCommand();
db.Insertable(new School_003() { scid = 2, schname = "青华" }).ExecuteCommand();
2022-07-03 03:18:39 +08:00
2022-07-03 04:23:35 +08:00
db.Insertable(new Room_003() { roomId = 1, schoolId = 1, roomName = "北大01室" }).ExecuteCommand();
db.Insertable(new Room_003() { roomId = 2, schoolId = 1, roomName = "北大02室" }).ExecuteCommand();
db.Insertable(new Room_003() { roomId = 3, schoolId = 2, roomName = "青华03室" }).ExecuteCommand();
db.Insertable(new Room_003() { roomId = 4, schoolId = 2, roomName = "青华04室" }).ExecuteCommand();
2022-07-03 03:18:39 +08:00
2022-07-03 04:23:35 +08:00
db.Insertable(new Desk_003() { roomId = 1, deskid = 1, deskName = "北大01室_01" }).ExecuteCommand();
db.Insertable(new Desk_003() { roomId = 2, deskid = 2, deskName = "北大02室_01" }).ExecuteCommand();
db.Insertable(new Desk_003() { roomId = 3, deskid = 3, deskName = "青华03室_01" }).ExecuteCommand();
db.Insertable(new Desk_003() { roomId = 4, deskid = 4, deskName = "青华04室_01" }).ExecuteCommand();
2022-07-03 03:18:39 +08:00
2022-07-03 04:23:35 +08:00
var list = db.Queryable<Student_003>()
2022-07-03 03:18:39 +08:00
.Includes(x => x.school_001, x => x.rooms, x => x.desk)
.Where(x => x.school_001.rooms.Any(z => z.desk.Any())).ToList();
if (list.Count() != 2)
{
throw new Exception("unit error");
}
2022-07-03 04:23:35 +08:00
var list2 = db.Queryable<Student_003>()
2022-07-03 03:18:39 +08:00
.Includes(x => x.school_001, x => x.rooms)
.Where(x => x.school_001.rooms.Any(z =>
z.roomName == "北大01室" &&
z.desk.Any())).ToList();
if (list2.Count() != 1)
{
throw new Exception("unit error");
}
2022-07-03 04:23:35 +08:00
var list3 = db.Queryable<Student_003>()
2022-07-03 03:18:39 +08:00
.Includes(x => x.school_001, x => x.rooms)
.Where(x => x.school_001.rooms.Any(z =>
z.roomName == "青华03室" &&
z.desk.Any(c => c.deskName == "青华03室_01"))).ToList();
if (list3.Count != 1)
{
throw new Exception("unit error");
}
2022-07-03 04:23:35 +08:00
var list4 = db.Queryable<Student_003>()
2022-07-03 03:18:39 +08:00
.Where(x => x.school_001.rooms.Any(z =>
z.roomName == "青华03室" &&
z.desk.Any(c => c.deskName == "青华04室_01"))).ToList();
if (list4.Count != 0)
{
throw new Exception("unit error");
}
2022-07-03 04:23:35 +08:00
db.DbMaintenance.TruncateTable<Student_003, School_003, Room_003, Desk_003>();
2022-07-03 03:18:39 +08:00
foreach (var item in list)
{
item.sid = 0;
item.SchoolId = 0;
item.school_001.scid = 0;
foreach (var r in item.school_001.rooms)
{
r.roomId = 0;
r.schoolId = 0;
foreach (var z in r.desk)
{
z.deskid = 0;
z.roomId = 0;
}
}
}
db.InsertNav(list.First())
2022-07-04 05:00:47 +08:00
.Include(x => x.school_001)
2022-07-03 03:18:39 +08:00
.ThenInclude(x => x.rooms)
2022-07-03 16:55:07 +08:00
.ThenInclude(x => x.desk).ExecuteCommand();
2022-07-03 03:18:39 +08:00
db.InsertNav(list.Last())
2022-07-04 05:00:47 +08:00
.Include(x => x.school_001)
2022-07-03 03:18:39 +08:00
.ThenInclude(x => x.rooms)
2022-07-03 16:55:07 +08:00
.ThenInclude(x => x.desk).ExecuteCommand();
2022-07-03 03:18:39 +08:00
2022-07-03 04:23:35 +08:00
if (db.Queryable<Desk_003>().Count() != 4 || db.Queryable<Room_003>().Count() != 4
|| db.Queryable<School_003>().Count() != 2 || db.Queryable<Student_003>().Count() != 2)
2022-07-03 03:18:39 +08:00
{
throw new Exception("unit error");
}
2022-07-03 03:24:51 +08:00
2022-07-03 04:23:35 +08:00
db.DbMaintenance.TruncateTable<Student_003, School_003, Room_003, Desk_003>();
2022-07-03 03:24:51 +08:00
db.InsertNav(list.First().school_001)
2022-07-04 05:00:47 +08:00
.Include(x => x.rooms)
2022-07-03 16:55:07 +08:00
.ThenInclude(x => x.desk).ExecuteCommand();
2022-07-03 03:24:51 +08:00
db.InsertNav(list.Last().school_001)
2022-07-04 05:00:47 +08:00
.Include(x => x.rooms)
2022-07-03 16:55:07 +08:00
.ThenInclude(x => x.desk).ExecuteCommand();
2022-07-03 03:28:31 +08:00
2022-07-03 04:23:35 +08:00
if (db.Queryable<Desk_003>().Count() != 4 || db.Queryable<Room_003>().Count() != 4
|| db.Queryable<School_003>().Count() != 2 || db.Queryable<Student_003>().Count() != 0)
2022-07-03 03:28:31 +08:00
{
throw new Exception("unit error");
}
2022-07-03 03:18:39 +08:00
}
2022-07-03 04:23:35 +08:00
public class Student_003
2022-07-03 03:18:39 +08:00
{
2022-07-03 04:23:35 +08:00
[SqlSugar.SugarColumn(IsPrimaryKey =true)]
public long sid { get; set; }
2022-07-03 03:18:39 +08:00
public string Name { get; set; }
2022-07-03 04:23:35 +08:00
public long SchoolId { get; set; }
2022-07-03 03:18:39 +08:00
[SqlSugar.Navigate(SqlSugar.NavigateType.OneToOne,nameof(SchoolId))]
2022-07-03 04:23:35 +08:00
public School_003 school_001 { get; set; }
2022-07-03 03:18:39 +08:00
}
2022-07-03 04:23:35 +08:00
public class School_003
2022-07-03 03:18:39 +08:00
{
2022-07-03 04:23:35 +08:00
[SqlSugar.SugarColumn(IsPrimaryKey = true )]
public long scid { get; set; }
2022-07-03 03:18:39 +08:00
public string schname { get; set; }
2022-07-03 04:23:35 +08:00
[SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany, nameof(Room_003.schoolId))]
public List<Room_003> rooms { get; set; }
2022-07-03 03:18:39 +08:00
}
2022-07-03 04:23:35 +08:00
public class Room_003
2022-07-03 03:18:39 +08:00
{
2022-07-03 04:23:35 +08:00
[SqlSugar.SugarColumn(IsPrimaryKey = true )]
public long roomId { get; set; }
public long schoolId { get; set; }
2022-07-03 03:18:39 +08:00
public string roomName { get; set; }
2022-07-03 04:23:35 +08:00
[SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany, nameof(Desk_003.roomId))]
public List<Desk_003> desk { get; set; }
2022-07-03 03:18:39 +08:00
}
2022-07-03 04:23:35 +08:00
public class Desk_003
2022-07-03 03:18:39 +08:00
{
2022-07-03 04:23:35 +08:00
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
public long deskid { get; set; }
2022-07-03 03:18:39 +08:00
public string deskName { get; set; }
2022-07-03 04:23:35 +08:00
public long roomId { get; set; }
2022-07-03 03:18:39 +08:00
}
}
}