TopBasePoco是框架所有模型的基類,它只定義了一個(gè)Guid的主鍵,所有其他模型都必須直接或間接繼承TopBasePoco,否則框架的其他自動(dòng)化操作無(wú)法執(zhí)行
這意味著框架只支持Guid為唯一主鍵的數(shù)據(jù)庫(kù)設(shè)計(jì),雖然不同的人對(duì)此有不同的看法,但其實(shí)任何表的設(shè)計(jì)都不妨礙再加一個(gè)Guid的主鍵
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在BasePoco的基礎(chǔ)上增加了IsValid字段,指示框架對(duì)這類模型進(jìn)行假刪除
框架自帶的刪除功能會(huì)自動(dòng)將繼承PersistPoco的模型的IsValid字段設(shè)為false,而不會(huì)真正刪除它
模型之間的一對(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ú)法正常工作
以下代碼創(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)系并生成正確的代碼
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Text;
- using WalkingTec.Mvvm.Core;
- namespace FirstProject.Model
- {
- public enum SchoolTypeEnum {
- [Display(Name = "公立學(xué)校")]
- PUB,
- [Display(Name = "私立學(xué)校")]
- PRI
- }
- public class School : BasePoco
- {
- [Display(Name = "學(xué)校編碼")]
- [Required(ErrorMessage = "{0}是必填項(xiàng)")]
- [RegularExpression("^[0-9]{3,3}$", ErrorMessage = "{0}必須是3位數(shù)字")]
- [StringLength(3)]
- public string SchoolCode { get; set; }
- [Display(Name = "學(xué)校名稱")]
- [StringLength(50, ErrorMessage = "{0}最多輸入{1}個(gè)字符")]
- [Required(ErrorMessage = "{0}是必填項(xiàng)")]
- public string SchoolName { get; set; }
- [Display(Name = "學(xué)校類型")]
- [Required(ErrorMessage = "{0}是必填項(xiàng)")]
- public SchoolTypeEnum? SchoolType { get; set; }
- [Display(Name = "備注")]
- public string Remark { get; set; }
- }
- public class Major : BasePoco
- {
- [Display(Name = "專業(yè)編碼")]
- [Required(ErrorMessage = "{0}是必填項(xiàng)")]
- [RegularExpression("^[0-9]{3,3}$", ErrorMessage = "{0}必須是3位數(shù)字")]
- public string MajorCode { get; set; }
- [Display(Name = "專業(yè)名稱")]
- [StringLength(50, ErrorMessage = "{0}最多輸入{1}個(gè)字符")]
- [Required(ErrorMessage = "{0}是必填項(xiàng)")]
- public string MajorName { get; set; }
- [Display(Name = "備注")]
- public string Remark { get; set; }
- [Display(Name = "所屬學(xué)校")]
- [Required()]
- public Guid? SchoolId { get; set; }
- [Display(Name = "所屬學(xué)校")]
- public School School { get; set; }
- [Display(Name = "學(xué)生")]
- public List<StudentMajor> StudentMajors { get; set; }
- }
- public class Student : BasePoco
- {
- [Display(Name = "賬號(hào)")]
- [Required(ErrorMessage = "{0}是必填項(xiàng)")]
- [StringLength(50, ErrorMessage = "{0}最多輸入{1}個(gè)字符")]
- public string LoginName { get; set; }
- [Display(Name = "密碼")]
- [Required(AllowEmptyStrings = false)]
- [StringLength(32)]
- public string Password { get; set; }
- [Display(Name = "姓名")]
- [Required(ErrorMessage = "{0}是必填項(xiàng)")]
- [StringLength(50, ErrorMessage = "{0}最多輸入{1}個(gè)字符")]
- public string Name { get; set; }
- [Display(Name = "手機(jī)")]
- [RegularExpression("^[1][3,4,5,7,8][0-9]{9}$", ErrorMessage = "{0}格式錯(cuò)誤")]
- public string CellPhone { get; set; }
- [Display(Name = "郵編")]
- [RegularExpression("^[0-9]{6,6}$", ErrorMessage = "{0}必須是6位數(shù)字")]
- public string ZipCode { get; set; }
- [Display(Name = "日期")]
- public DateTime? EnRollDate { get; set; }
- [Display(Name = "專業(yè)")]
- public List<StudentMajor> StudentMajor { get; set; }
- }
- [MiddleTable]
- public class StudentMajor : BasePoco
- {
- public Major Major { get; set; }
- public Student Student { get; set; }
- public Guid MajorId { get; set; }
- public Guid StudentId { get; set; }
- }
- }
-
更多建議: