EF4 自引用关联 0..1 -> 1
关于自引用问题有很多阅读,但我似乎找不到我的问题的答案。
假设我有一个人类(A),并且我希望 A 有一个伙伴,即另一个人类(B)。自然,这意味着B有人类A的伴侣。你会如何解决这个问题?理想情况下,我只需要这样做:
humanA.Partner = humanB;
并且 humanB 会自动让 humanA 作为合作伙伴。
我本以为我可以创建一个人类实体,并添加一个协会,例如:
End1 实体:人类,多重性:0..1,导航属性:合作伙伴
End2 实体:人类,多重性:0..1
因此,每个人都有零个或一个人类伙伴。
感谢您抽出时间。
There's a lot of reading on self referencing problems, but I can't seem to find an answer to my question.
Say I have a Human(A), and I want A to have a partner, another Human(B). Naturally, it means that B has a partner in human A. How would you solve this? Ideally, I should only have to do:
humanA.Partner = humanB;
and humanB would automatically get humanA as a partner.
I would have thought I could create a Human enity, and add an Association, something like:
End1 Entity:Human, Multiplicity:0..1, Navigation Property:Partner
End2 Entity:Human, Multiplicity:0..1
So, each human has zero or one Partner which is a human.
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当 实体“共享”主键<时,1:0..1 映射才可能/a>.这意味着当相关的实体FK也是它的PK时。所以自引用 1:0..1 不可能存在。我认为你甚至不能直接将它映射到数据库中。
1:0..1 mapping is possible only when entities "share" primary key. It means when the related's entity FK is also its PK. So self referencing 1:0..1 cannot exist. I think you can't even map it in database directly.