First said, this is my first experience using the Entity Framework so I apologize if this ends up being a newbie question. Everything has gone well so far; but, I run into a situation that I am not sure what the best way to handle is.
I need to create a many-to-many relationship; but, the many-to-many table contains more than just the composite key. In this case the Entity Framework does not see to recognize it as a many-to-many structure and so I don’t seem to get the easy ability to get the collections without using the intervening table. Is there a better way to do this?
Simplified Example:
- A Unit can have many boards and a board could have been in many units
- When it is in a unit we need to record what slot it was in.
Tables:
Unit
UnitID(PK)
UnitName
Board
BoardID(PK)
BoardName
UnitBoard
UnitID(PK,FK1)
BoardID(PK,FK2)
Slot
When I pull this into my code using an ADO.NET Entity Data Model, I don’t see an easy way to get the collection of Boards associated to a Unit from the Unit Entity or visa-versa.
Is there a better way to do this or do I just need to use the associated collection of UnitBoards then use that to build the collection of Units/Boards?
This seems I am probably not the first person to went to do something like this. Ex: I was thinking of Books and Owners wanting to also keep the related BookOwner information (like the purchase date).
Even in the updated Entity Framework 5, there is no good solution for this problem. If you have the proper Foreign Key relationships you should be able to access your Boards or Units like:
or
but you won’t have a direct navigation properties (many-to-many navigations) if the many-to-many table has it’s own properties outside of the composite key.
When filtering you can include them using
IncludeandSelectto reduce db calls.