Navigation Properties for Many to Many relationship

Thread is closed for posting
2 posts, 0 answers
  1. Gary
    Gary avatar
    28 posts
    Member since:
    Jun 2015

    Posted 10 Aug 2015 Link to this post

    I am working with an existing database which I was able to import into a Domain Model (rling) file. As this is new, greenfield development I am continually adding and revising the tables and their relationships (which by the way, has been made very easy with this ORM!) and am frequently updating the model from the database ... anyway ... I have a situation where I have created a many to many relationship in the database. It involves a PRODUCTS table, a PACKAGES table, and a join table which allows me to define multiple products in a single package and a product in multiple packages. I was expecting to see a navigation property, in the domain model, on the PRODUCT model, which would allow me to list the packages that the product was participating in ... PRODUCT.PACKAGES, and vice versa ... PACKAGE.PRODUCTS. Instead each model has a ​navigation property ​which references the ​join table instead. PRODUCT.PRODUCT_PACKAGES_JOIN and PACKAGE.PRODUCT_PACKAGES_JOIN (pseudo-code). Is there a way to add navigational properties to each end of the relationship so I do not have to go through the join table?


    I found this article ... How to: Manage Many-to-Many Associations... but the example it gave appeared to be some configuration code, which I have not yet had to involve myself with, so I do not know where or how that would work into the project.


    Any help would be greatly appreciated! Thanks!

  2. Boris Georgiev
    Boris Georgiev avatar
    190 posts

    Posted 13 Aug 2015 Link to this post

    Hello Gary,

    When you add a new join table in the database, the table could be recognized as a basic table instead of join table and because of that in the model it will occur as a class with two associations. In this case, you should manually create the Many-to-Many association in the Domain Model.

    To do that, you could follow these steps:
    1) Delete the Domain Class associated with the Join table from the Domain Model.
    2) Open Model Schema Explorer and find the Join table.
    3) From the Properties window change IsJoinTable property to True.
    4) Create an association between the two classes and open the association for edit with the Association Editor.
    5) In RelationalView check Use Join Table check-box and select the Join Table from the drop down.
    6) Manually Map the Ids from the tables to the Join table.

    Then you will have Many-to-Many association between the two classes.

    I hope that helps.

    Boris Georgiev
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Back to Top