You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SqlSugar;
  6. using OrmTest.Demo;
  7. namespace OrmTest.Demo
  8. {
  9. public class ComplexModel : DemoBase
  10. {
  11. public static void Init()
  12. {
  13. var db = GetInstance();
  14. var students = db.Queryable<CMStudent>().ToList();
  15. if (students != null)
  16. {
  17. foreach (var item in students)
  18. {
  19. Console.WriteLine(item.SchoolName);
  20. if (item.SchoolSingle != null)
  21. {
  22. Console.WriteLine(item.SchoolSingle.Name);
  23. }
  24. if (item.SchoolList != null)
  25. {
  26. Console.WriteLine(item.SchoolList.Count);
  27. }
  28. }
  29. }
  30. }
  31. }
  32. [SugarTable("Student")]
  33. public class CMStudent : ModelContext
  34. {
  35. public int Id { get; set; }
  36. public string Name { get; set; }
  37. public int SchoolId { get; set; }
  38. [SugarColumn(IsIgnore = true)]
  39. public string SchoolName
  40. {
  41. get
  42. {
  43. if (this.SchoolSingle != null)
  44. return this.SchoolSingle.Name;
  45. else
  46. return null;
  47. }
  48. }
  49. [SugarColumn(IsIgnore = true)]
  50. public CMSchool SchoolSingle
  51. {
  52. get
  53. {
  54. return base.CreateMapping<CMSchool>().Single(it => it.Id == this.SchoolId);
  55. }
  56. }
  57. [SugarColumn(IsIgnore = true)]
  58. public List<CMSchool> SchoolList
  59. {
  60. get
  61. {
  62. return base.CreateMapping<CMSchool>().Where(it => it.Id == this.SchoolId).ToList();
  63. }
  64. }
  65. }
  66. [SugarTable("School")]
  67. public class CMSchool
  68. {
  69. public int Id { get; set; }
  70. public string Name { get; set; }
  71. }
  72. }