Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为ef core中的同一实体设置外键_C#_Entity Framework Core - Fatal编程技术网

C# 为ef core中的同一实体设置外键

C# 为ef core中的同一实体设置外键,c#,entity-framework-core,C#,Entity Framework Core,我想为实体框架核心中的同一实体类构建父-子实体 所以我有一个这样的实体: public class Definition { public int Id{ get; set; } public int ParentId { get; set; } public string Name { get; set; } public bool Active { get; set; } } 我想将ParentId设置为外键,该外键引用另一个定义实体作为父实体。 我该怎么做

我想为
实体框架核心
中的同一实体类构建
父-子
实体

所以我有一个这样的实体:

public class Definition
{
    public int Id{ get; set; }
    public int ParentId { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }
}
我想将
ParentId
设置为外键,该外键引用另一个
定义
实体作为父实体。 我该怎么做


谢谢为父定义添加外键属性

 public class Definition
 {  
    [Key]
    public int Id{ get; set; }

    public int? ParentId { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }

    [ForeignKey("ParentId")]
    public virtual Definition ParentDefinition { get; set; }
}

这回答了你的问题吗?