【发布时间】:2020-06-05 19:18:37
【问题描述】:
我的目标是以树形格式列出具有母办公室的办公室,以便在财务报告中作为下拉列表。我将 .NET Core 2.1 与 Entity Framework Core 一起使用,并通过 Web API 公开数据。
我追求的结果:
A
A1
A2
A2A
B
B1
B1A
B2
这是我目前的模型:
public class OfficeSymbol
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public bool Inactive { get; set; }
public int ParentId { get; set; }
[Required]
public int Level { get; set; }
}
我试图使用int Level 来了解孩子的嵌套深度。
我还尝试了以下方法以及Cascade.NoAction,但实体框架不允许键约束:
public class OfficeSymbol
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public bool Inactive { get; set; }
public int ParentId { get; set; }
[Required]
public int Level { get; set; }
[ForeignKey("ParentId")]
public OfficeSymbol Parent { get; set; }
[InverseProperty("Parent")]
public ICollection<OfficeSymbol> Children { get; set; }
}
任何帮助将不胜感激。
【问题讨论】:
-
我想我找到了问题所在。我的对象已经创建并试图将其从上面的代码修改为下面的示例。我创建了一个新项目,并且能够动态创建较低的对象。所以我相信它要么不能将现有表修改为较低的对象,要么在其他表的其他地方存在依赖关系。
标签: c# .net-core entity-framework-core hierarchical-data