views:

30

answers:

1

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 feels like that should not be needed.

+2  A: 

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

Alex James
Thanks for the help. I was thinking that you have two nodes A and B that are stored and there is a relationship between them. I only want to store that relationship once. But I want to know about the relationship from node A and from node B. So it is stored once but represented in two places, the two ends of the relationship.
Phil Wright
Storing it once, but representing it in multiple places is what I was suggesting. In fact that is what that blog post does.
Alex James