Inherit vs FK

Thread is closed for posting
3 posts, 1 answers
  1. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 18 Apr 2013 Link to this post


    Is it possible to have something like this:

    1) Table Contacts (entity Contact)
    2) Table Suppliers (Entity Supplier)
    3) Supplier inherits from Contact
    Now I can add a Contact "Erik".
    I can add a Supplier "Telerik". This will generate a discriminator value in contact.

    Q: Can I make "Erik" a supplier? So:
    Dim C As Contact = GetContact(1) (= "Erik" )
    Dim S As New Supplier
    S.ContactId = C.ContactId
    S.SupplierType = "Hoster"

    If I try this with my model, I get an error about the discriminator value; so, OA thinks retrieving the Suppliers is an error because of this.

    Is the only solution a FK? (No inhertiance and using Supplier.Contact.Name)


  2. Answer
    Kaloyan Nikolov
    Kaloyan Nikolov avatar
    118 posts

    Posted 20 Apr 2013 Link to this post

    Hi Erik,

    If you have a model with inheritance and you want to add a Supplier entity to the database you don't need to instantiate first a Contact and then the Supplier. You can instantiate the Supplier entity and store it, like this:
    Dim S As New Supplier
    S.Name = "Telerik"
    S.SupplierType = "Hoster"

    If you want to convert the an existing entity from Contact to Supplier you should change the discriminator value for that object. You can achieve this using Stored Procedure which changes the discriminator value directly for the entity in the database. You can map the Stored Procedure as a Domain Method using OpenAccess ORM and call it when/where you need. After you change the discriminator value you will be able to select the same entity with the same Id from the Suppliers collection instead of the Contacts collection.

    The Stored Procedure should look like this:
    CREATE PROCEDURE ChangeDiscriminatorValue
        @entityId int,
        @newDisciriminator int
        UPDATE Contact SET voa_class = @newDisciriminator WHERE id = @entityId
    * voa_class is the auto-generated column for the class discriminator, if you are using different column in your mapping, update the SP code above.

    You cannot achieve the same behavior in the way you are trying because you actually create a second object with the same Id and different discriminator value and violate the table constraints, that's why you receive the exception. One and the same instance cannot co-exists as both types in the table. 

    If this approach is not acceptable for you then you should change from Inheritance to FK.

    I hope this helps. Do not hesitate to contact us again with further questions.

    Kind regards,
    Kaloyan Nikolov
    the Telerik team
    Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
  3. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 22 Apr 2013 Link to this post

    Thanks Kaloyan for your extensive explanation. Helped a lot!
Back to Top