EF-如何定义外键,即当前表的主键

时间:2019-08-03 12:32:43

标签: c# entity-framework asp.net-core

我的表名是具有列名的Grades升级引用的值,它将是表中的主键值之一。Grades-同一表- 我尝试像下面的代码那样做

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

1 个答案:

答案 0 :(得分:0)

[ForeignKey]属性对自引用FK的效果应该与常规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; }
}