How to get Entity to Table mapping for many, many relationships from a metamodel in the Enity Framework

Is there any way to get a many-to-many base table connecting two objects in an entity structure?

For example, if we have Product and Order objects, how can we get the base connection table Product_Order, which combines the table Product and Order.

Any help would be appreciated.

+7
source share
1 answer

You can set the connection table as an object, but it is not general / necessary:

  • You will start with this:

enter image description here

  • Remove many-to-many relationships between Product and Order objects
  • Create a new ProductOrder object in the designer (either using the toolbar or the context menu)
  • Define two properties in your new entity that correspond to the foreign keys defined in your join table - ProductId , OrderId - make sure that both of them are marked as an entity key (they must be a compound primary key in the database) and have the same type as PKs
  • Open map information and map the new entity to the connection table.

enter image description here

  • Create two new one-to-many associations. Frist between Product and ProductOrder , and the second between Order and ProductOrder .
  • In the properties window of each added relationship, reference restrictions are set ( as described here) .

enter image description here

There is a very high probability that if you need it, you are doing something wrong.

+9
source

All Articles