code first关系表达
1.一对多关系
[Table("classInfo")] public class ClassInfo { public int Id { get; set; } public string ClassName { get; set; } public virtual ICollection<Student> Students { get; set; } }
[Table("student")] public class Student { [Key] [Column("studentId", TypeName ="int")] public int StudentId { get; set; } [Column("name", TypeName = "varchar")] [MaxLength(20)] public string Name { get; set; } [Column("password", TypeName = "varchar")] [MaxLength(20)] public string Password { get; set; } [Column("birthday", TypeName = "datetime")] //让EF不在追踪这个属性(手动修改数据库的字段),放在最后 //[DatabaseGenerated(DatabaseGeneratedOption.Computed)] public DateTime Birthday { get; set; } [Column("height", TypeName = "float")] public double Height { get; set; } //public virtual List<Course> Courses { get; set; } public virtual ClassInfo ClassInfo { get; set; } }