Is there any way we can get the underlying "many to many table" joi开发者_JS百科ning the two entities in entity framework.
For example if we have Product and Order entities, how can we get the underlying conjunction table Product_Order that joins the Product and Order table.
Any help would be appreciated.
It is possible to expose junction table as an entity but it is not common / needed:
- You will start with this:
- Delete many-to-many relation between your
Product
andOrder
entities - Create new
ProductOrder
entity in designer (either by using toolbox or context menu) - Define two properties in your new entity corresponding to Foreign keys defined in your junction table -
ProductId
,OrderId
- make sure that both are marked as entity key (they must be composite primary key in the database) and have the same type as PKs - Open Mapping details and map your new entity to junction table
- Create two new one-to-many associations. The frist between
Product
andProductOrder
and the second betweenOrder
andProductOrder
. - In Property window of each added relation set up referential constraints (as described also here).
There is very big chance that if you need this you are doing something wrong.
精彩评论