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

Inherit vs FK

2 Answers 45 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.
Erik
Top achievements
Rank 2
Erik asked on 18 Apr 2013, 08:23 AM
Hello,

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"
Connection.Add(S)
Connection.SaveChanges

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)

Erik





2 Answers, 1 is accepted

Sort by
0
Accepted
Kaloyan Nikolov
Telerik team
answered on 20 Apr 2013, 05:08 AM
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"
Connection.Add(S)
Connection.SaveChanges

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
AS
BEGIN
    UPDATE Contact SET voa_class = @newDisciriminator WHERE id = @entityId
END
* 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 >>
0
Erik
Top achievements
Rank 2
answered on 22 Apr 2013, 10:40 AM
Thanks Kaloyan for your extensive explanation. Helped a lot!
Tags
Development (API, general questions)
Asked by
Erik
Top achievements
Rank 2
Answers by
Kaloyan Nikolov
Telerik team
Erik
Top achievements
Rank 2
Share this question
or