layris3 / Daily-learning-experience-record

daily learning experience
0 stars 0 forks source link

EF-foreign key in entity class #20

Open layris3 opened 4 years ago

layris3 commented 4 years ago

EF interprets a property as a foreign key property if it's named <navigation property name>+ <navigation property's primary key name> or <navigation property's primary key name>

for example,

    public class Enrollment
    {
        public int EnrollmentID { get; set; }
        public int StudentID { get; set; }
        public virtual Student Student { get; set; }
    }

we know that the primary key in Student class is ID,so the StudentID property satisfies the first pattern of two patterns mentioned above so it is the foreign key property.

on the other hand,if the primary key in Student class is StudentID,the StudentID property is still the foreign key since it satisfies the <navigation property's primary key name> pattern.