layris3 / Daily-learning-experience-record

daily learning experience
0 stars 0 forks source link

EF-primary key in entity class #19

Open layris3 opened 4 years ago

layris3 commented 4 years ago

by default,EF interprets a property that's named ID or classname ID as a primary key property. For example, classnameID pattern

    public class Enrollment
    {
        public int EnrollmentID { get; set; }
        ...
    }

ID pattern

    public class Enrollment
    {
        public int ID { get; set; }
        ...
    }
layris3 commented 4 years ago

on the other hand,if your property doesn't satisfy the pattern above but you wanna regard it as the primaty key. [Key]attribute provide a way to set anyone property to be the primary key. for example,

    public class Enrollment
    {
        [Key]
        public int TestID { get; set; }
        ...
    }