[英]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 我的表名是Grades,其中的列名为Upgrade指向值,该值将是表Grades中主键的值之一-同一张表,我尝试执行以下代码
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 [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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.