我的模型如下:
public class Category : BaseEntity
{
public string Name { get; set; }
public virtual Category Parent { get; set; }
public string Description { get; set; }
}
Parent属性与同一张表相关。我该如何配置?
我在想像这样的东西:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Category>()
.HasOne(a => a.Parent)
.OnDelete(false);
}
我是实体框架核心的新手,请帮帮我!
答案 0 :(得分:0)
解决者:
public class Category : BaseEntity
{
public string Name { get; set; }
public int? ParentId{ get; set; } /*added*/
[ForeignKey("ParentId")] /*added*/
public virtual Category Parent { get; set; }
public string Description { get; set; }
}