使用实体框架核心时,请防止循环父母/子数据

发布于 2025-02-05 03:27:46 字数 347 浏览 2 评论 0原文

给定一个数据模型,其中包含对其父级的引用(邻接列表):

class Foo
{
   public int Id { get; set; }
   virtual Foo Parent { get; set; }
}

我如何保证不会对数据库进行任何环状引用?

好的:

a
  b
    c

不好:

a
  b
    c
      a

这是我可以通过设置隔离级别(可序列化)来做的吗?这是使用数据库本身中某种触发器完成的最好的做法吗?我应该使用不同的模型作为分层数据吗?

Given a data model which contains reference to its parent (adjacency list):

class Foo
{
   public int Id { get; set; }
   virtual Foo Parent { get; set; }
}

How can I guarantee that no cyclic references will be committed to the database?

ok:

a
  b
    c

NOT ok:

a
  b
    c
      a

Is this something I can do by setting the isolation level (to Serializable)? Is this best done using some sort of trigger in the database itself? Should I be using a different model for my hierarchical data?

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

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

发布评论

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

评论(1

浊酒尽余欢 2025-02-12 03:27:46

如果我正确理解您的问题,我认为将以下内容放在“父”类中是足够的:

    [JsonIgnore] //if you will ever Serialize it
    [ForeignKey("OrderProductionNumber")] //using System.ComponentModel.DataAnnotations.Schema;
    public virtual Foo Parent { get; set; }

If I understand your problem correctly, I think it is enough to put the following in the "Parent" class:

    [JsonIgnore] //if you will ever Serialize it
    [ForeignKey("OrderProductionNumber")] //using System.ComponentModel.DataAnnotations.Schema;
    public virtual Foo Parent { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文