OpenAccess Mapping one-to-one ?

9 posts, 0 answers
  1. Seabus
    Seabus avatar
    33 posts
    Member since:
    Jun 2011

    Posted 26 Jul 2011 Link to this post

    Hi,

    Is it possible to map a one to one relationship using Telerik Open Access ?

    Sebastian
  2. Ivailo
    Admin
    Ivailo avatar
    318 posts

    Posted 28 Jul 2011 Link to this post

    Hello Seabus,


    Yes, Telerik OpenAccess ORM can map a one to one relationship. The way it is modeled, however, is not as 1:1 association in the Domain Model, but as a vertical inheritance between the two classes involved.

    There are two things to have in mind when implementing such a relationship as a vertical inheritance:
    1. To choose the proper inheritance direction, make sure that the child class corresponds to the source table of the 1:1 foreign key reference. In other words, the parent class should correspond to the table which the database will allow us to insert a row in before inserting a row in the other table.
    2. The child class should not contain the primary key that is targeted by the 1:1 relationship. It will use the key from the parent class directly. After you synchronize the Domain Model with the database, delete this primary key property or properties from the diagram and save it.

    We are soon releasing a Knowledge Base article regarding this relationship implementation with a thorough example. Once it is published I am going to post a link in this forum thread for your convenience.

    Do not hesitate to contact us in case you need any clarification.


    Greetings,
    Ivailo
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  3. DevCraft banner
  4. Ivailo
    Admin
    Ivailo avatar
    318 posts

    Posted 03 Aug 2011 Link to this post

    Hi Sebastian,

    As promised we are notifying you about the release of the Knowledge Base article "How to map a One to One relationship" which might prove useful to you.

    http://www.telerik.com/support/kb/orm/general/how-to-map-a-one-to-one-relationship.aspx

    Greetings,
    Ivailo
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  5. Rob Jones
    Rob Jones avatar
    1 posts
    Member since:
    Aug 2011

    Posted 23 Aug 2011 Link to this post

    Hello,

    Isn't it a big limitation that we can't map a one-to-one relationship in "the normal" way?

    I will give a quick example. A while ago, I was on a quest to find a way that would allow me to develop a generic repository and unit of work approach that would allow me to switch between ORM frameworks fairly easily with an absolute minimum amount of code changes. I used the EntityFramework and NHibernate as starting points to see if a common ground could be found.

    I will admit that this might be a fairly unusual situation. Nobody should want to switch ORM framework when in the middle of developing a large enterprise application, but it did happen to me and that motivated me to research this particular problem. In the end I developed a fairly nice solution (in my opinion, that is... :D).

    Today I decided to add OpenAccess support to my framework solution and found there's a bit of a problem when it comes to mapping one-to-one relationships. In EntityFramework and NHibernate a one-to-one relationship is very straight forward. The relationship is resembled by a (navigation) property. To my disappointment I found that OpenAccess does not have this straight forward one-to-one mapping.

    In my case this is absolutely game breaking. This means that my (domain) model would look different from a model built on EntityFramework or NHibernate. Obviously this means that any code previously written against the EF or NH model would no longer be working with an OA domain model.

    I can imagine it would make sense to use inheritance in this scenario. However, to stay in line with all the other ORM frameworks that I have used, I would have liked to see a more conventional approach that keeps the navigation property in tact without inheritance. Is that at all possible in OpenAccess at the moment?

    Kind regards,
    Rob Jones
  6. Ivailo
    Admin
    Ivailo avatar
    318 posts

    Posted 25 Aug 2011 Link to this post

    Hello Rob,

    We agree that what OpenAccess ORM is providing at the moment for one to one relationships is not straight forward as expected by some customers, especially ones that are more used to design their databases before their models. We are thinking of improving that and offer better user experience for this kind of relation for some of our future releases.

    For the time being, the closest thing you can do in OpenAccess ORM if inheritance doesn't make sense for your scenario is to model the relationship as one to many association - refer to the KB article for details. Again, not very straight forward, but a working approach. I hope it will be useful for your solution.


    Regards,
    Ivailo
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

  7. Pierre
    Pierre avatar
    7 posts
    Member since:
    Jun 2010

    Posted 12 Dec 2012 Link to this post

    Is there something new about 1 to 1 relationships at the moment ??

    I need to create a 1 to 1 between two entities that are completly un-relative. I got CUSTOMER and CIRCUIT. A CUSTOMER can have one and only one CIRCUIT, a CIRCUIT can only correspond to one CUSTOMER.

    implement vertical Inheritance between those two entities is a non-sense for me because CUSTOMER will never be a Parent of a CIRCUIT.

    Thx for answers !!
  8. Ivailo
    Admin
    Ivailo avatar
    318 posts

    Posted 12 Dec 2012 Link to this post

    Hello Pierre,

    Yes, in fact we have.

    The native 1:1 relationship is now supported by Telerik OpenAccess ORM. If you have such constraint in your database and you create a model based on it, OpenAccess will recognize the relation and create a 1:1 relationship. On the other hand, you can also setup the relationship manually, by creating an association between the identity properties of the two domain classes. Download the latest Q3 2012 SP1 release and enjoy this and plenty of other new features we have introduced since Q2 2011.

    Here you can find more information regarding how we support 1:1 associations now.

    I am looking forward to your feedback and I hope the new association type will be useful in your projects.

    Greetings,
    Ivailo
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
  9. Pierre
    Pierre avatar
    7 posts
    Member since:
    Jun 2010

    Posted 13 Dec 2012 Link to this post

    Thx a lot for this quick answer !

    So i'm trying to build a 1 to 1. First i build it on sql server like that :

    http://www.hostingpics.net/viewer.php?id=257626Sql.png

    I set auto increment on both Table and then i just create one foreign key between the two Primary key of each table.

    Then i use "the Update from Database" in the model and i got this :

    http://www.hostingpics.net/viewer.php?id=325773Model.png

    Seems like OpenAccess map it to a composition (because of the white rectangle) and i got this error :

    The source class of the 'TESTHasTEST2' 1:1 association uses backend calculated identity. This is not allowed because the foreign key value should be obtained from the identity of the target class. Consider defining the association in the opposite direction.

    What am i doing wrong ??

    Again, thx a lot for your answer !!
  10. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 17 Dec 2012 Link to this post

    Hello Pierre,

    The classes on the diagram look correctly mapped, however the exception message says what the actual problem is.
    Normally associations (1:N for example) are created on a foreign key and a primary key column, where the foreign key value is obtained from the primary key value of the referenced record/object. 1:1 associations are additionally limited because the primary key of the referencing table is also used as foreign key to the referenced table. In this case, if the primary key of the referencing table is Autoinc, we cannot update its value and assign the PK value from the referenced table.
    Such setup could work if both tables are empty and we always insert records in both tables, so the incremental keys are synchronized. But this this scenario would be better implemented by just using one table, not two.
    Most of the scenarios that require 1:1 associations also need to be able to update the association between the records. This is not possible with an autoinc primary key used as foreign key.

    In order to define a 1:1 association in an OpenAccess model, please make sure that the primary key of the referencing table is not autoinc. OpenAccess will do the inserts and updates in this column automatically based on the object relations set in your code.

    Please do not hesitate to contact us if you need more details or assistance with the implementation.

    Kind regards,
    Alexander
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
Back to Top
DevCraft banner