EF4 Self referencing with association 0..1 -> 1

2019-07-12 12:13发布

问题:

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:

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.