Many-to-many with both direct links and query access to mapping table?

Thread is closed for posting
3 posts, 1 answers
  1. Fredrik
    Fredrik avatar
    8 posts
    Member since:
    Sep 2014

    Posted 14 Sep 2014 Link to this post

    I wonder if it's possible to setup a many-to-many relationship that both supports direct links between to major entities as well as query access to the mapping table?
    My current association looks like this:

    HasAssociation(x => x.Tags).WithOpposite(x => x.Pages).MapJoinTable("PageTag", (x, y) => new { x.PageId, y.TagId }).CreatePrimaryKeyFromForeignKeys();

    ..and mirrored in other mapping as:

    HasAssociation(x => x.Pages).WithOpposite(x => x.Tags);

    This works great, I can access Tag.Pages and Page.Tags. But I would also like to be able to query the join table directly without getting the major entities (in other words just the id's that it consists of). Is it possible to do that somehow and still keep the direct links? I tried to add a join-entity and map it to the table but since it's already defined above it throws an error.

    Grateful for any help :)
  2. Answer
    Boris Georgiev
    Boris Georgiev avatar
    190 posts

    Posted 17 Sep 2014 Link to this post

    Hello Fredrik,

    Unfortunately it is not possible to define a table as join table and to execute queries directly to the table. If you want to execute queries to the JoinTable you should define the association as 1:m:1 and map the JoinTable to a class.

    You can easily get the ids of all entities in one relation from many to many association:
    1.Page page = context.Pages.FirstOrDefault();
    2.IEnumerable<int> tagsId = page.Tags.Select(c => c.Id);

    I hope that helps. If any questions arise do not hesitate to contact us again.

    Boris Georgiev
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
  3. Fredrik
    Fredrik avatar
    8 posts
    Member since:
    Sep 2014

    Posted 18 Sep 2014 in reply to Boris Georgiev Link to this post

    Thanks for your answer! I'll be switching over to a 1:m:1 structure.
Back to Top