简体   繁体   中英

EF - How to define foreign key which is the primary key of the current table

I have table name is Grades which have a column name Upgrade refer to value which is will be one of the values of the primary key in the table Grades - same table- I try to do this like the following code

public class Grades
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Grades UpGrade { get; set; }
        public int UpGradeId { get; set; }
    }

[ForeignKey] attribute should work just as well for self-referencing FKs as it works for regular ForeignKeys

public class Grades
{
    public int Id { get; set; }
    public string Name { get; set; }
    [ForeignKey("UpGradeId")]
    public Grades UpGrade { get; set; }
    public int UpGradeId { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM