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類型