Create relationship by key

4 posts, 1 answers
  1. Emiliano
    Emiliano avatar
    6 posts
    Member since:
    Nov 2013

    Posted 18 Nov 2013 Link to this post

    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
  2. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 20 Nov 2013 Link to this post

    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!
  3. DevCraft banner
  4. Emiliano
    Emiliano avatar
    6 posts
    Member since:
    Nov 2013

    Posted 20 Nov 2013 Link to this post

    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
  5. Answer
    Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 25 Nov 2013 Link to this post

    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!
Back to Top