This is a migrated thread and some comments may be shown as answers.

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

2 Answers 44 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Fredrik
Top achievements
Rank 2
Fredrik asked on 14 Sep 2014, 05:59 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Boris Georgiev
Telerik team
answered on 17 Sep 2014, 05:14 PM
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.

Regards,
Boris Georgiev
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
0
Fredrik
Top achievements
Rank 2
answered on 18 Sep 2014, 04:46 PM
Thanks for your answer! I'll be switching over to a 1:m:1 structure.
Tags
General Discussions
Asked by
Fredrik
Top achievements
Rank 2
Answers by
Boris Georgiev
Telerik team
Fredrik
Top achievements
Rank 2
Share this question
or