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; }
}
这回答了你的问题吗?