EF Code-First 学习之旅 一对多的关系
public class Student { public Student() { } public int StudentId { get; set; } public string StudentName { get; set; } public virtual Standard Standard { get; set; } } public class Standard { public Standard() { Students = new List<Student>(); } public int StandardId { get; set; } public string Description { get; set; } public virtual ICollection<Student> Students { get; set; } }
上面的代码中,Student实体包含导航属性Standard,Standard实体包含集合导航属性Student,Code First的默认规则为1对多的关系
指定外键
public class Student { public Student() { } public int StudentId { get; set; } public string StudentName { get; set; } public int StdandardRefId { get; set; } [ForeignKey("StandardRefId")] public virtual Standard Standard { get; set; } } public class Standard { public Standard() { Students = new List<Student>(); } public int StandardId { get; set; } public string Description { get; set; } public virtual ICollection<Student> Students { get; set; } }
Fluent API配置
public class Student { public Student(){ } public int StudentId { get; set; } public string StudentName { get; set; } public int StandardId { get; set; } public virtual Standard Standard { get; set; } } public class Standard { public Standard() { StudentsList = new List<Student>(); } public int StandardId { get; set; } public string Description { get; set; } public virtual ICollection<Student> Students { get; set; } }
protected override void OnModelCreating(DbModelBuilder modelBuilder) { //one-to-many modelBuilder.Entity<Student>() .HasRequired<Standard>(s => s.Standard) // Student entity requires Standard .WithMany(s => s.Students); // Standard entity includes many Students entities }
如果外键不符合默认规则
public class Student { public Student(){ } public int StudentId { get; set; } public string StudentName { get; set; } //StdId is not following code first conventions name public int StdId { get; set; } public virtual Standard Standard { get; set; } } public class Standard { public Standard() { StudentsList = new List<Student>(); } public int StandardId { get; set; } public string Description { get; set; } public virtual ICollection<Student> Students { get; set; } }
可以如下配置
protected override void OnModelCreating(DbModelBuilder modelBuilder) { //one-to-many modelBuilder.Entity<Student>() .HasRequired<Standard>(s => s.Standard) .WithMany(s => s.Students) .HasForeignKey(s => s.StdId); }
modelBuilder.Entity<Student>().HasRequired<Standard>(s => s.Standard)
表明Student必须包含Standard导航属性,
.WithMany(s => s.Students).HasForeignKey(s => s.StdId)表明Standard有多个Student,外键名为StdId。
另一种方法
protected override void OnModelCreating(DbModelBuilder modelBuilder) { //configure one-to-many modelBuilder.Entity<Standard>() .HasMany<Student>(s => s.Students) Standard has many Students .WithRequired(s => s.Standard) Student require one Standard .HasForeignKey(s => s.StdId);Student includes specified foreignkey property name for Standard }
外键可空的配置
protected override void OnModelCreating(DbModelBuilder modelBuilder) { //one-to-many modelBuilder.Entity<Student>() .HasOptional<Standard>(s => s.Standard) .WithMany(s => s.Students) .HasForeignKey(s => s.StdId); }
配置如发生如下异常
One or more validation errors were detected during model generation:
Domain.Student_Standard: : Multiplicity conflicts with the referential constraint in Role 'Student_Standard_Target' in relationship 'Student_Standard'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.
则需要配置外键为nullable类型