简体   繁体   English

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

[英]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.

相关问题 使用EF将表的主键映射为AspNetUser表作为外键 - Mapping of primary key of a table into AspNetUser Table as foreign key using EF 同一主键ApplicationUser表上的EF多个外键关系 - EF multiple foreign key relationship on same primary key ApplicationUser table 相同表EF6中对主键的外键引用 - Foreign Key References to Primary Key in the Same Table EF6 如何处理子表中的主键也是父表中的主键和外键? - How to handle primary key in child table which is also primary and foreign key in parent table? 在EF中使用复合主键作为外键 - Use Composite Primary key as Foreign key in EF 外键作为EF Code First中的主键? - Foreign key as primary key in EF Code First? 如何更新在另一个表中作为外键引用的表的主键? - How to Update the primary key of table which is referenced as foreign key in another table? 流利的API,将主键定义为外键 - Fluent API, define a primary key as foreign key 如何同时创建将成为外键和主键的字段 - How to make a field which will be foreign key and primary key at the same time EF的代码优先,如何在不设置数据库表之间外键的情况下定义导航属性关系 - Code first of EF, how to define navigation property relationship without setting foreign key between table in database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM