MVC-CodeFirst(一)
最近刚做完一个Web的项目,不过也是第一次接触MVC,做完以后对MVC朦朦胧胧,所以在此就查了好多资料,看了好多大牛的博客,也不知道最近为什么这么多MVC系列,不过刚好有助于我的学习,顺便自己也记录一些学到的东西,不过还是有很多不懂的地方,如果有大牛路过,请指点一二。
CodeFirst
建了三个项目,分别是DAL(DB层),ModelEntities(实体层),MyWeb.EntitiesConfiguration(实体配置层)。
ModelEntities
简单的建立了几个表,主要是想分别体现一对多,多对多的特征和写法。
User实体
1 [Table("t_User")] 2 public class User 3 { 4 [Key] 5 [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 6 public int Id { get; set; } 7 8 public string Name { get; set; } 9 10 public string UserName { get; set; } 11 12 public string Password { get; set; } 13 14 public virtual ICollection<Employee> CreatorList { get; set; } 15 16 public virtual ICollection<Employee> ModifiedPersonList { get; set; } 17 }
Employee实体
1 [Table("t_Employee")] 2 public class Employee 3 { 4 public Employee() 5 { 6 this.WorkRecordList = new HashSet<WorkRecord>(); 7 this.ContractList = new HashSet<Contract>(); 8 this.EnrollmentList = new HashSet<Enrollment>(); 9 } 10 11 12 [Key] 13 [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 14 public int Id { get; set; } 15 16 [StringLength(20,ErrorMessage="员工姓名最大长度为50!")] 17 [Display(Name="员工姓名")] 18 public string EmployeeName { get; set; } 19 20 [StringLength(20,ErrorMessage="联系方式最长为20!")] 21 [Display(Name= "员工联系方式")] 22 public string EmployeePhone { get; set; } 23 24 25 [ForeignKey("Creator")] 26 public int CreatorId { get; set; } 27 28 //ON DELETE NO ACTION 或 ON UPDATE NO ACTION 29 [ForeignKey("ModifiedPerson")] 30 public int ModifiedPersonId { get; set; } 31 32 public virtual User Creator { get; set; } 33 34 35 public virtual User ModifiedPerson { get; set; } 36 37 public virtual ICollection<Enrollment> EnrollmentList { get; set; } 38 39 public virtual ICollection<Contract> ContractList { get; set; } 40 41 public virtual ICollection<WorkRecord> WorkRecordList { get; set; } 42 }
Job实体
1 [Table("t_Job")] 2 public class Job 3 { 4 [Key] 5 [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 6 public int Id { get; set; } 7 8 [Display(Name = "职位名称")] 9 public string JobName { get; set; } 10 11 public virtual ICollection<Enrollment> EnrollmentList { get; set; } 12 }
Enroollment实体
1 [Table("t_Enrollment")] 2 /// <summary> 3 /// 关系(员工和职位,多对多的关系) 4 /// </summary> 5 public class Enrollment 6 { 7 [Key] 8 [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 9 public int Id { get; set; } 10 11 /// <summary> 12 /// 职工Id 13 /// </summary> 14 [ForeignKey("Employee")] 15 [Display(Name="员工Id")] 16 public int EmployeeId { get; set; } 17 18 [ForeignKey("Job")] 19 public int JobId { get; set; } 20 21 22 public virtual Employee Employee { get; set; } 23 24 public virtual Job Job { get; set; } 25 }
WorkRecord实体
1 /// <summary> 2 /// 一对多(Employee) 3 /// </summary> 4 [Table("t_WorkRecord")] 5 public class WorkRecord 6 { 7 [Key] 8 [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 9 public int Id { get; set; } 10 11 [ForeignKey("Employee")] 12 public int EmployeeId { get; set; } 13 14 public DateTime CreatedDate { get; set; } 15 16 /// <summary> 17 /// 不能包含“@”,并不能为空 18 /// </summary> 19 [ValidationAttributeHelper] 20 public string WorkContent { get; set; } 21 22 public virtual Employee Employee { get; set; } 23 }
Contract实体
1 /// <summary> 2 /// 合同表(一对多Employee) 3 /// </summary> 4 [Table("t_Contract")] 5 public class Contract 6 { 7 [Key] 8 [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 9 public int Id { get; set; } 10 11 public string ContractName { get; set; } 12 13 [ForeignKey("Employee")] 14 public int EmployeeId { get; set; } 15 16 public virtual Employee Employee { get; set; } 17 }
各个配置的含义
[Table("t_User")]该表在数据库中的名称为“t_User”;
[Key]该字段为主键;
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]该字段为自增;
[ForeignKey("Employee")]该字段为外键,对应虚的属性public virtual Employee Employee { get; set; },也就是该字段是Employee的外键;
[Display(Name="员工姓名")]该字段显示的内容;
[StringLength(20,ErrorMessage="员工姓名最大长度为20!")]该字段的长度,如果超出,提示相应的错误;
1 [ForeignKey("Creator")] 2 public int CreatorId { get; set; } 3 public virtual User Creator { get; set; }
属性Creator是外键User,可以没有CreatorId这个字段,数据库会自动创建一个名为“Creator_Id”的字段,设为外键,与User管理,不过不建议这样做,一方面命名不是自己的,另一方面使用不方便;
1 public virtual ICollection<Employee> CreatorList { get; set; }
Employee表与User关联的同时,也需要在User表里建立User一对多属性,否则会建立失败;
[ValidationAttributeHelper]该字段需符合该规则,这个规则是自己定义的,也是在看一位大牛的文章的时候看到的,就自己也写了一个,具体代码:
1 /// <summary> 2 /// 创建已定义验证 3 /// </summary> 4 public class ValidationAttributeHelper : ValidationAttribute 5 { 6 protected override ValidationResult IsValid(object value, ValidationContext validationContext) 7 { 8 //检查是否为空值 9 if (value == null) 10 { 11 return new ValidationResult("不能为空!"); 12 } 13 else 14 { 15 if (value.ToString().Contains("@")) 16 { 17 return new ValidationResult("不能包含@符号"); 18 } 19 } 20 return ValidationResult.Success; 21 } 22 }
先到这吧~