开发者

Can Entity Framework map two associations into a single navigation?

I have two simple tables as described here...

Table = Person
   PersonID  (int, PrimaryKey)
   FirstName (char)
   LastName  (char)

Table = Related
   RelatedID    (int, PrimaryKey)
   Person1      (int, ForeignKey for Person.PersonID)
   Person2      (int, ForeignKey for Person.PersonID)
   Relationship (int)

The generated entity for Person has two navigation collections. One for the Related.Person1 and another for Related.Person2. This is a pain because it means I have two collections to investigate to look up all the relationships that are relevant to that person.

I need to have instead just a single navigation collections that contains both of these sets. Is it possible to have this generated as part of the entity frameowrk? The only alternative is to generate a third collection myself that contains the aggregate set of entities and it f开发者_开发问答eels like that should not be needed.


As Craig states what you are asking for is not something core to EF but... a friendship type relationship is one of those nasty ones to model, so I see what you are trying to do.

There is a workaround to make this possible by mapping the Association Set to a view (DefiningQuery) in the SSDL.

The association/relationship backed by the view will then be read-only, so you will probably want to leave the other two relationships in place to allow you to add / remove from the correct collection or for when you want to search in one direction only.

Check out this post which shows the techniques involved for more.

Hope this helps

Alex

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜