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

Create relationship by key

3 Answers 60 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Emiliano
Top achievements
Rank 1
Emiliano asked on 18 Nov 2013, 05:00 PM
There are some scenarios where I want to generate a new relationship but I already know the Id (key) of the related object and I don't want to load it to generate it.

Say we have the following:

Posts
* Id
* Title

Tags
* Id
* Name

PostTags
* PostId
* TagId

So Posts can have many tags.

2 questions then:

1) If I already know the PostId and TagId is there a way to generate that entry in the database without having to load both the Post and Tag object?

2) If not, is there a way to at least load just one? For example, loading just the Post object and do something like Post.Tags.AddByKey(tagId).

Thanks

3 Answers, 1 is accepted

Sort by
0
Doroteya
Telerik team
answered on 20 Nov 2013, 12:04 PM
Hi Emiliano,

Generally, Telerik OpenAccess ORM allows you to implement a scenario where records are inserted directly in a join table (in a many-to-many relationship) without loading the related objects first. In order to meet this requirement you need to know the values of the keys in advance, and to perform a change in the model that will allow you to issue statements against the join table. Below you can find the recommended workflow:

1) Open the .rlinq file in Visual Designer.
2) Delete the many-to-many association between the Posts and Tags entities.
3) Open Model Schema Explorer and expand the Tables node.
4) Find the PostTags table and drag-and-drop it on the designer's surface.
5) Save the .rlinq file

At this point you can perform inserts in the PostTags table as described here.

Regarding the idea for the AddByKey() method, I would strongly advise you to post it as a feature request on our Ideas and Feedback portal. There the developers who use OpenAccess ORM can vote about it and once it gathers the approval of the community we are going to consider a place for it on our roadmap.

I hope this helps. If you need further assistance or have additional questions, do not hesitate to get back to us.


Regards,
Doroteya
Telerik
OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
0
Emiliano
Top achievements
Rank 1
answered on 20 Nov 2013, 03:50 PM
Thank you for your answer.

Unfortunately doing what you suggested removes the ability to use post.Tags or tag.Posts which I do want to keep. I was asking for a way to add the relationship knowing the keys but without deleting the association.

I'll post the AddByKey idea to the feature requests though.

Thanks
0
Accepted
Doroteya
Telerik team
answered on 25 Nov 2013, 12:47 PM
Hello Emiliano,

Thank you for your feedback.

As an alternative approach, I would suggest to you our Low Level (ADO) API. With its help you can execute any custom SQL statement you need, using ADO like code. Utilizing it will help you achieve the necessary result on one hand, and preserve the navigation properties on the other. The workflow is described step-by-step and with an example in this documentation article.

I hope this helps. If you need further assistance or have additional questions, do not hesitate to get back to us.

Regards,
Doroteya
Telerik
OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
Tags
Development (API, general questions)
Asked by
Emiliano
Top achievements
Rank 1
Answers by
Doroteya
Telerik team
Emiliano
Top achievements
Rank 1
Share this question
or