开发者

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" 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:

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

  • 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

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

  • 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).

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜