Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Entity to Table mapping for many to many relations from the metamodel in Enity Framework

Is there any way we can get the underlying "many to many table" joining 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.

like image 804
Baig Avatar asked May 06 '11 11:05

Baig


1 Answers

It is possible to expose junction table as an entity but it is not common / needed:

  • You will start with this:

enter image description here

  • Delete many-to-many relation between your Product and Order 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

enter image description here

  • Create two new one-to-many associations. The frist between Product and ProductOrder and the second between Order and ProductOrder.
  • In Property window of each added relation set up referential constraints (as described also here).

enter image description here

There is very big chance that if you need this you are doing something wrong.

like image 88
Ladislav Mrnka Avatar answered Oct 24 '22 03:10

Ladislav Mrnka