創(chuàng)建模型

2019-04-04 17:40 更新
TopBasePoco

TopBasePoco是框架所有模型的基類,它只定義了一個(gè)Guid的主鍵,所有其他模型都必須直接或間接繼承TopBasePoco,否則框架的其他自動(dòng)化操作無(wú)法執(zhí)行

這意味著框架只支持Guid為唯一主鍵的數(shù)據(jù)庫(kù)設(shè)計(jì),雖然不同的人對(duì)此有不同的看法,但其實(shí)任何表的設(shè)計(jì)都不妨礙再加一個(gè)Guid的主鍵

BasePoco

BasePoco是框架所有內(nèi)置模型的基類,它在TopBasePoco的基礎(chǔ)上增加了創(chuàng)建人,修改人,創(chuàng)建時(shí)間和修改時(shí)間四個(gè)字段,對(duì)于大多數(shù)模型,這些字段都是需要的

建議用戶自定義的模型都繼承BasePoco

框架自帶的添加和修改操作會(huì)自動(dòng)更新創(chuàng)建人,修改人,創(chuàng)建時(shí)間和修改時(shí)間這些字段

PersistPoco

PersistPoco在BasePoco的基礎(chǔ)上增加了IsValid字段,指示框架對(duì)這類模型進(jìn)行假刪除

框架自帶的刪除功能會(huì)自動(dòng)將繼承PersistPoco的模型的IsValid字段設(shè)為false,而不會(huì)真正刪除它

一對(duì)多和多對(duì)多

模型之間的一對(duì)多和多對(duì)多的關(guān)系完全遵從EF Core默認(rèn)的規(guī)則

初次使用者容易忘記定義外鍵名稱,比如定義了一個(gè)指向School類的屬性叫做ParentSchool,那么必須有一個(gè)叫做ParentSchoolId的guid屬性定義外鍵名稱,這是EF Core的默認(rèn)規(guī)則

如果不定義這個(gè)加Id的字段,雖然數(shù)據(jù)庫(kù)可以生成,但是在代碼層面是不知道外鍵名稱的,框架的代碼生成器也就無(wú)法正常工作

其他
框架除了要求模型必須最終繼承自TopBasePoco之外,沒(méi)有其他額外要求。只要遵循EF Core的方式創(chuàng)建模型類就可以
示例

以下代碼創(chuàng)建了一個(gè)學(xué)校模型,一個(gè)專業(yè)模型和一個(gè)學(xué)生模型

學(xué)校和專業(yè)之間是一對(duì)多的關(guān)系,學(xué)生和專業(yè)之間是多對(duì)多的關(guān)系,通過(guò)中間表StudentMajor關(guān)聯(lián)

請(qǐng)注意這些關(guān)聯(lián)關(guān)系的定義,比如School和ShoolId,Major和MajorId,Student和StudentId這些都是成對(duì)出現(xiàn)的

StudentMajor模型上的[MiddleTable]標(biāo)記,表示這是一張多對(duì)多的中間表,在中間表上加[MiddleTable]可以讓代碼生成器識(shí)別多對(duì)多的關(guān)系并生成正確的代碼

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Text;
  5. using WalkingTec.Mvvm.Core;
  6. namespace FirstProject.Model
  7. {
  8. public enum SchoolTypeEnum {
  9. [Display(Name = "公立學(xué)校")]
  10. PUB,
  11. [Display(Name = "私立學(xué)校")]
  12. PRI
  13. }
  14. public class School : BasePoco
  15. {
  16. [Display(Name = "學(xué)校編碼")]
  17. [Required(ErrorMessage = "{0}是必填項(xiàng)")]
  18. [RegularExpression("^[0-9]{3,3}$", ErrorMessage = "{0}必須是3位數(shù)字")]
  19. [StringLength(3)]
  20. public string SchoolCode { get; set; }
  21. [Display(Name = "學(xué)校名稱")]
  22. [StringLength(50, ErrorMessage = "{0}最多輸入{1}個(gè)字符")]
  23. [Required(ErrorMessage = "{0}是必填項(xiàng)")]
  24. public string SchoolName { get; set; }
  25. [Display(Name = "學(xué)校類型")]
  26. [Required(ErrorMessage = "{0}是必填項(xiàng)")]
  27. public SchoolTypeEnum? SchoolType { get; set; }
  28. [Display(Name = "備注")]
  29. public string Remark { get; set; }
  30. }
  31. public class Major : BasePoco
  32. {
  33. [Display(Name = "專業(yè)編碼")]
  34. [Required(ErrorMessage = "{0}是必填項(xiàng)")]
  35. [RegularExpression("^[0-9]{3,3}$", ErrorMessage = "{0}必須是3位數(shù)字")]
  36. public string MajorCode { get; set; }
  37. [Display(Name = "專業(yè)名稱")]
  38. [StringLength(50, ErrorMessage = "{0}最多輸入{1}個(gè)字符")]
  39. [Required(ErrorMessage = "{0}是必填項(xiàng)")]
  40. public string MajorName { get; set; }
  41. [Display(Name = "備注")]
  42. public string Remark { get; set; }
  43. [Display(Name = "所屬學(xué)校")]
  44. [Required()]
  45. public Guid? SchoolId { get; set; }
  46. [Display(Name = "所屬學(xué)校")]
  47. public School School { get; set; }
  48. [Display(Name = "學(xué)生")]
  49. public List<StudentMajor> StudentMajors { get; set; }
  50. }
  51. public class Student : BasePoco
  52. {
  53. [Display(Name = "賬號(hào)")]
  54. [Required(ErrorMessage = "{0}是必填項(xiàng)")]
  55. [StringLength(50, ErrorMessage = "{0}最多輸入{1}個(gè)字符")]
  56. public string LoginName { get; set; }
  57. [Display(Name = "密碼")]
  58. [Required(AllowEmptyStrings = false)]
  59. [StringLength(32)]
  60. public string Password { get; set; }
  61. [Display(Name = "姓名")]
  62. [Required(ErrorMessage = "{0}是必填項(xiàng)")]
  63. [StringLength(50, ErrorMessage = "{0}最多輸入{1}個(gè)字符")]
  64. public string Name { get; set; }
  65. [Display(Name = "手機(jī)")]
  66. [RegularExpression("^[1][3,4,5,7,8][0-9]{9}$", ErrorMessage = "{0}格式錯(cuò)誤")]
  67. public string CellPhone { get; set; }
  68. [Display(Name = "郵編")]
  69. [RegularExpression("^[0-9]{6,6}$", ErrorMessage = "{0}必須是6位數(shù)字")]
  70. public string ZipCode { get; set; }
  71. [Display(Name = "日期")]
  72. public DateTime? EnRollDate { get; set; }
  73. [Display(Name = "專業(yè)")]
  74. public List<StudentMajor> StudentMajor { get; set; }
  75. }
  76. [MiddleTable]
  77. public class StudentMajor : BasePoco
  78. {
  79. public Major Major { get; set; }
  80. public Student Student { get; set; }
  81. public Guid MajorId { get; set; }
  82. public Guid StudentId { get; set; }
  83. }
  84. }


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)