使用实体框架核心时,请防止循环父母/子数据
给定一个数据模型,其中包含对其父级的引用(邻接列表):
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您的问题,我认为将以下内容放在“父”类中是足够的:
If I understand your problem correctly, I think it is enough to put the following in the "Parent" class: