EF4自身参考与关联0..1 - > 1 [英] EF4 Self referencing with association 0..1 -> 1

查看:158
本文介绍了EF4自身参考与关联0..1 - > 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于自我引用问题的阅读,但我似乎找不到我的问题的答案。



说我有一个(A),我想要A有一个合作伙伴,另一个人(B)。当然,这意味着B在人类中有一个伙伴A.你会如何解决这个问题?理想情况下,我只需要做:

  humanA.Partner = humanB; 

,并且humanB将自动将manA作为合作伙伴。


$ b $我会以为我可以创造一个人类,并添加一个协会,如:


End1 实体:人类,多重性: 0..1 ,导航属性:合作伙伴



End2 实体:人类,多重性: 0..1


所以,每个人都有零个或一个合作伙伴是人类。



感谢你的时间。

解决方案

1:0..1只有当实体共享主键。这意味着相关实体FK也是其PK。所以自我参考1: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.

解决方案

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.

这篇关于EF4自身参考与关联0..1 - > 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆