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

240 lines
11 KiB
C#
Raw Normal View History

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-19 14:03:14 +08:00
// db.CodeFirst.InitTables<StudentA, RoomA, SchoolA, TeacherA>();
// db.CodeFirst.InitTables<BookA>();
// db.DbMaintenance.TruncateTable<StudentA>();
// db.DbMaintenance.TruncateTable<RoomA>();
// db.DbMaintenance.TruncateTable<SchoolA>();
// db.DbMaintenance.TruncateTable<TeacherA>();
// db.DbMaintenance.TruncateTable<BookA>();
// 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:27:47 +08:00
2022-04-19 14:03:14 +08:00
// db.Insertable(new SchoolA() { SchoolId = 1, SchoolName = "北大" }).ExecuteCommand();
// db.Insertable(new SchoolA() { SchoolId = 2, SchoolName = "清华" }).ExecuteCommand();
2022-04-10 01:27:47 +08:00
2022-04-19 14:03:14 +08:00
// 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();
2022-04-10 01:27:47 +08:00
2022-04-19 14:03:14 +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
2022-04-19 14:03:14 +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 11:03:55 +08:00
2022-04-11 20:54:20 +08:00
2022-04-19 14:03:14 +08:00
// db.Insertable(new BookA() { BookId=1, Names = "java" , studenId=1 }).ExecuteCommand();
// db.Insertable(new BookA() { BookId = 2, Names = "c#2", studenId = 2 }).ExecuteCommand();
// db.Insertable(new BookA() { BookId = 3, Names = "c#1", studenId = 2 }).ExecuteCommand();
// db.Insertable(new BookA() { BookId = 4, Names = "php", studenId = 3 }).ExecuteCommand();
// db.Insertable(new BookA() { BookId = 5, Names = "js", studenId = 4 }).ExecuteCommand();
2022-04-11 20:54:20 +08:00
2022-04-13 14:05:51 +08:00
2022-04-19 14:03:14 +08:00
// var list2 = db.Queryable<StudentA>()
//.Includes(x => x.SchoolA, x => x.RoomList)//2个参数就是 then Include
//.Includes(x => x.SchoolA, x => x.TeacherList)//2个参数就是 then Include
//.Includes(x => x.Books)
//.Where(x=>x.Books.Any(z=>z.BookId==1))
//.Where(x => x.SchoolA.SchoolName.Contains("北大"))
//.ToList();
2022-04-13 14:05:51 +08:00
2022-04-18 15:17:11 +08:00
var list3 = db.Queryable<StudentA>()
.Includes(x => x.SchoolA, x => x.RoomList)//2个参数就是 then Include
.Includes(x => x.SchoolA, x => x.TeacherList)//2个参数就是 then Include
2022-04-19 14:03:14 +08:00
.Includes(x => x.Books.Select(z=>new BookA() { Names = z.Names }).ToList())
2022-04-18 15:17:11 +08:00
.Where(x => x.Books.Any(z => z.BookId == 1))
2022-04-19 14:03:14 +08:00
.Where(x => x.SchoolA.School_Name == "北大")
2022-04-18 15:17:11 +08:00
.ToList();
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 21:19:51 +08:00
2022-04-11 14:04:22 +08:00
2022-04-13 14:05:51 +08:00
2022-04-11 21:19:51 +08:00
db.CodeFirst.InitTables<A1, B1, ABMapping1>();
db.DbMaintenance.TruncateTable<A1>();
db.DbMaintenance.TruncateTable<B1>();
db.DbMaintenance.TruncateTable<ABMapping1>();
db.Insertable(new A1() { Id = 1, Name = "a1" }).ExecuteCommand();
db.Insertable(new A1() { Id = 2, Name = "a2" }).ExecuteCommand();
2022-04-13 19:06:51 +08:00
db.Insertable(new A1() { Id = 3, Name = "a3" }).ExecuteCommand();
2022-04-11 21:19:51 +08:00
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();
2022-04-12 00:27:34 +08:00
db.Insertable(new ABMapping1() { AId =2, BId = 1 }).ExecuteCommand();
2022-04-11 21:19:51 +08:00
db.Insertable(new ABMapping1() { AId = 2, BId = 2 }).ExecuteCommand();
2022-04-14 19:22:48 +08:00
var p = "";
2022-04-18 15:17:11 +08:00
var list4= db.Queryable<A1>()
2022-04-14 19:22:48 +08:00
.Includes(x => x.BList.WhereIF(!string.IsNullOrEmpty(p),it=>it.Id==11).ToList())
2022-04-13 19:06:51 +08:00
.Where(x=>x.BList.Any()).ToList();
2022-04-13 21:10:02 +08:00
2022-04-12 08:26:09 +08:00
var list31 = db.Queryable<A1>().Includes(x => x.BList,x=>x.AList).ToList();
2022-04-12 01:30:21 +08:00
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();
2022-04-12 01:43:08 +08:00
db.Insertable(new Tree1() { Id = 6, Name = "020101", ParentId = 5 }).ExecuteCommand();
2022-04-14 13:23:07 +08:00
db.Insertable(new Tree1() { Id = 7, Name = "02010101", ParentId = 6 }).ExecuteCommand();
2022-04-13 01:03:43 +08:00
var list21111 = new List<Tree1>();
2022-04-13 17:40:15 +08:00
var xxx= db.Queryable<Tree1>()
2022-04-14 13:23:07 +08:00
.Includes(it => it.Child,it=>it.Child,it=>it.Child)
.Includes(it => it.Parent,it=>it.Parent,it=>it.Parent)
//.Where(it=>it.Child.Any())
2022-04-13 17:40:15 +08:00
.ToList();
2022-04-13 01:03:43 +08:00
2022-04-13 17:40:15 +08:00
db.ThenMapper(xxx, it =>
{
it.Child = it.Child.OrderBy(x => x.Id).ToList();
});
2022-04-12 01:30:21 +08:00
//var json = db.Utilities.SerializeObject(list4);
2022-04-12 13:02:00 +08:00
db.CodeFirst.InitTables<UnitA001, UnitA002>();
db.DbMaintenance.TruncateTable<UnitA001>();
db.DbMaintenance.TruncateTable<UnitA002>();
db.Insertable(new UnitA001() { id = 1, name1 = "a", orgid = "1" }).ExecuteCommand();
db.Insertable(new UnitA002() { id = 1, name2= "a2", orgid = "1" }).ExecuteCommand();
var list5=db.Queryable<UnitA001>().ToList();
db.ThenMapper(list5, it =>
{
it.UnitA002 = db.Queryable<UnitA002>().SetContext(x => x.orgid, () => it.id, it).First();
});
2022-04-11 20:54:20 +08:00
}
2022-04-10 01:27:47 +08:00
2022-04-12 13:02:00 +08:00
public class UnitA001
{
public int id { get; set; }
public string name1 { get; set; }
public string orgid { get; set; }
[SugarColumn(IsIgnore =true)]
public UnitA002 UnitA002 { get; set; }
}
public class UnitA002
{
public int id { get; set; }
public string name2{ get; set; }
public string orgid { get; set; }
}
2022-04-12 01:30:21 +08:00
public class Tree1
{
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
public int Id { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
2022-04-14 12:53:53 +08:00
[Navigate(NavigateType.OneToOne,nameof(ParentId))]
2022-04-12 01:30:21 +08:00
public Tree1 Parent { get; set; }
2022-04-14 12:53:53 +08:00
[Navigate(NavigateType.OneToMany,nameof(Tree1.ParentId))]
2022-04-12 01:30:21 +08:00
public List<Tree1> Child { get; set; }
}
2022-04-11 21:19:51 +08:00
public class ABMapping1
2022-04-11 20:54:20 +08:00
{
2022-04-11 21:19:51 +08:00
[SugarColumn(IsPrimaryKey = true )]
2022-04-11 20:54:20 +08:00
public int AId { get; set; }
2022-04-12 00:27:34 +08:00
[SugarColumn(IsPrimaryKey = true)]
2022-04-11 20:54:20 +08:00
public int BId { get; set; }
}
2022-04-11 21:19:51 +08:00
public class A1
2022-04-11 20:54:20 +08:00
{
2022-04-11 21:19:51 +08:00
[SugarColumn(IsPrimaryKey = true )]
2022-04-11 20:54:20 +08:00
public int Id { get; set; }
public string Name { get; set; }
2022-04-14 12:53:53 +08:00
[Navigate(typeof(ABMapping1),nameof(ABMapping1.AId),nameof(ABMapping1.BId))]
2022-04-11 21:19:51 +08:00
public List<B1> BList { get; set; }
2022-04-10 01:27:47 +08:00
}
2022-04-11 21:19:51 +08:00
public class B1
2022-04-11 20:54:20 +08:00
{
2022-04-11 21:19:51 +08:00
[SugarColumn(IsPrimaryKey = true )]
2022-04-11 20:54:20 +08:00
public int Id { get; set; }
public string Name { get; set; }
2022-04-14 12:53:53 +08:00
[Navigate(typeof(ABMapping1), nameof(ABMapping1.BId), nameof(ABMapping1.AId))]
2022-04-12 08:26:09 +08:00
public List<A1> AList { get; set; }
2022-04-11 20:54:20 +08:00
}
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-14 12:53:53 +08:00
[Navigate(NavigateType.OneToOne, nameof(SchoolId))]
2022-04-10 01:27:47 +08:00
public SchoolA SchoolA { get; set; }
2022-04-14 12:53:53 +08:00
[Navigate(NavigateType.OneToMany, nameof(BookA.studenId))]
2022-04-11 14:04:22 +08:00
public List<BookA> Books { get; set; }
2022-04-10 01:27:47 +08:00
2022-04-11 20:54:20 +08:00
}
2022-04-10 01:27:47 +08:00
public class SchoolA
{
[SugarColumn(IsPrimaryKey = true)]
public int SchoolId { get; set; }
2022-04-19 14:03:14 +08:00
[SugarColumn( ColumnName = "SchoolName")]
public string School_Name { get; set; }
2022-04-14 12:53:53 +08:00
[Navigate(NavigateType.OneToMany,nameof(RoomA.SchoolId))]
2022-04-10 01:27:47 +08:00
public List<RoomA> RoomList { get; set; }
2022-04-18 13:35:00 +08:00
[Navigate(NavigateType.OneToMany, nameof(TeacherA.SchoolId))]
2022-04-10 11:03:55 +08:00
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 20:54:20 +08:00
public class BookA
{
[SugarColumn(IsPrimaryKey = true)]
public int BookId { get; set; }
2022-04-19 14:03:14 +08:00
[SugarColumn(ColumnName ="Name")]
public string Names{ get; set; }
2022-04-11 14:04:22 +08:00
public int studenId { get; set; }
}
2022-04-10 01:27:47 +08:00
}
}