控制实体框架中的参考?

发布于 2025-01-20 14:15:08 字数 898 浏览 4 评论 0原文

在此处输入图像描述在实体框架中控制一个参考

opentity问题。

您认为什么是在MVC模式下工作的最有效方法,具有1个单一实体来引用本身。示例

 public class ClassGroupAndSubgroup
 {
    
     public int ID { get; set; }

    public int SubGroupId { get; set; }

    public string Name{ get; set; }

}

ClassGroupAndSubgroup principalGroup { SubGroupId = 0, Name ="Principal"}; ----> Id = 1 (AutoNum)
ClassGroupAndSubgroup SubGroup { SubGroupId = 1, Name ="Item_1_Subgroup1"}; ----> Id = 2 (AutoNum)
ClassGroupAndSubgroup SubGroup_Two { SubGroupId = 1, Name ="Item_2_Subgroup1"}; ----> Id = 3 (AutoNum)
ClassGroupAndSubgroup principalGroup { SubGroupId = 3, Name ="Item_1_Subgroup2"};----> Id = 4 (AutoNum)

我想要的是自身进行引用,但要从EntityFramework Core中进行引用。很久以前,我用C#和SQL中的算法做到了,但我无法在实体中做到这一点

enter image description hereControl a reference in entity framework

Open question .Net Core 5 - Entities.

What do you think is the most efficient way to work in MVC mode, having 1 single entity that references itself. example

 public class ClassGroupAndSubgroup
 {
    
     public int ID { get; set; }

    public int SubGroupId { get; set; }

    public string Name{ get; set; }

}

ClassGroupAndSubgroup principalGroup { SubGroupId = 0, Name ="Principal"}; ----> Id = 1 (AutoNum)
ClassGroupAndSubgroup SubGroup { SubGroupId = 1, Name ="Item_1_Subgroup1"}; ----> Id = 2 (AutoNum)
ClassGroupAndSubgroup SubGroup_Two { SubGroupId = 1, Name ="Item_2_Subgroup1"}; ----> Id = 3 (AutoNum)
ClassGroupAndSubgroup principalGroup { SubGroupId = 3, Name ="Item_1_Subgroup2"};----> Id = 4 (AutoNum)

What I want is to make reference on itself, but from EntityFramework Core. I did it a long time ago with algorithms in C# and SQL but I can't do it in entity

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懷念過去 2025-01-27 14:15:08

您希望外键属性引用父项,而不是子项,例如

 public class Foo
 {
    
    public int ID { get; set; }

    public int ParentFooId{ get; set; }

    public Foo Parent {get;set;}

    public string Name{ get; set; }
}

You want the Foreign Key property to refer to the parent, not the child, eg

 public class Foo
 {
    
    public int ID { get; set; }

    public int ParentFooId{ get; set; }

    public Foo Parent {get;set;}

    public string Name{ get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文