设想有以下几个类 省 包含 市 类的导航,市 包含街道类的导航,如何一次查询出所有导航数据?又如 学生实体类内包含学校,学校实体类中包含房间,支持一对一和一对多导航,请看代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
public class StudentA { [SugarColumn(IsPrimaryKey = true)] public int StudentId { get; set; } public string Name { get; set; } public int SchoolId { get; set; } [Navigate(NavigateType.OneToOne, nameof(SchoolId))]//一对一 public SchoolA SchoolA { get; set; } [Navigate(NavigateType.OneToMany, nameof(BookA.studenId))]//一对多 public List<BookA> Books { get; set; }//只能是null不能赋默认值 } public class SchoolA { [SugarColumn(IsPrimaryKey = true)] public int SchoolId { get; set; } public string SchoolName { get; set; } [Navigate(NavigateType.OneToMany, nameof(RoomA.SchoolId))]//一对多 public List<RoomA> RoomList { get; set; }//只能是null不能赋默认值 } public class RoomA { [SugarColumn(IsPrimaryKey = true)] public int RoomId { get; set; } 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; } } var list2 = db.Queryable<StudentA>() //查2层 .Includes(st => st.SchoolA, sch=> sch.RoomList)//查询2级(等于EF ThenInclude) //查1层 .Includes(st=> st.Books) .ToList() //说明: 一对多 多对多 一对多 只要配好了都可以多层级使用 //如果想超过3个层级需要.AsNavQueryable() //缺点VS提示会消失,直接写不要在乎意提示不出来,VS关掉在开就行了,只要不改这个代码提示就不会有问题 db.Queryable<Order>().AsNavQueryable().Includes(it=>it.1,it=>it.2,it=>it.3,it=>it.4,it=>it.5..) //.AsNavQueryable()能不用尽量不要用,正常Includes(+3)重载完全够用了 |