"How to: Model Complex Inheritance" problem (VB)

7 posts, 1 answers
  1. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 11 Apr 2012 Link to this post

    Hi,                                                        

    (ORM v2012.1.301.2)

    Following the documentation here:
    http://www.telerik.com/help/openaccess-orm/openaccess-tasks-define-model-inheritance-mixed-mapping.html 

    This generates errors (2) and the code generation (1) is wrong:
    ....
    End Class
    Public Partial Class Cat
         Inherits Pet Implements ICat
        Private _breed As String
        Public Overridable Property Breed As String
    ....
    .... ^^ VB really wants the "Implements ICat" on the next line you know...

    ... Also I get 6 other errors
    1) Interface 'ICat' is not implemented by this class.
    2) The IdentityType of the 'ConsoleApplication2.Cat' is not compatible with the number of its identity fields.
    3) The IdentityType of the 'ConsoleApplication2.Dog' is not compatible with the number of its identity fields.
    4) The IdentityType of the 'ConsoleApplication2.Pet' is not compatible with the number of its identity fields.
    5) The IdentityType of the 'ConsoleApplication2.Rottweiler' is not compatible with the number of its identity fields.
    6) The IdentityType of the 'ConsoleApplication2.WienerDog' is not compatible with the number of its identity fields.


    What I would like to have is this something like this:

    Contact
        ContactId: Int32 (pk, autonr)
        Name: String
        Type: Int32

    Employee
        EmployeeId: Int32 (pk, autonr)
        ContactId: Int32 (fk > Contact.ContactID)
        Phone: String

    So: With my model I want to do this:

    Using dbContext As New EntitiesModel()
        Dim Emp As New Employee
        With Emp
            .Name = "Erik"
            .Type = 1
            .Phone = "123456789"
        End With
        dbContext.Add(cat)
     
        dbContext.SaveChanges()
    End Using

    When I do this ^ the model automatically creates a Contact record with ContactId=1 (autonr) AND creates a Employee record with EmployeeId=1 (autonr) and ContactId=1 (bind to contact fk) ...

    Or am I expecting to much? :-)

    Thanks,

    Erik


  2. Answer
    Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 12 Apr 2012 Link to this post

    Hello Erik,

     You are right, there are some problems with interfaces implementation via the visual designer, especially with VB .NET. However the scenario you are trying to achieve is totally doable via the vertical inheritance feature of Telerik OpenAccess ORM. The structure of your properties and columns will not be absolutely the same but logically you will get the same behavior. 

    With vertical inheritance, you can have one ContactId property on the base Contact class. In the database, there will two tables, each having a ContactId column, related in a 1:1 relationship. When you insert a new employee object, data will be written accordingly in ther Employee and the Contact tables. The ContactId column will contain the same value of 1 for example and in your object model you will be able to obtain that value via the derived property Employee.ContactId.

    I have attached a sample project where the above explained is implemented. You should only run the Update Database From Model Wizard in order to create your local database and the code in the Main method that is identical to the one you proposed will run and produce the expected results.

    Greetings,
    Zoran
    the Telerik team
    Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
  3. DevCraft banner
  4. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 12 Apr 2012 Link to this post

    Got it to work Zoran, 

    Thanks!
  5. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 15 Jul 2012 Link to this post

    Well, got an fluent mapping model working now, thanks for all the feedback!

    In relation to this post I have 2 questions:

    The model you provide creates an Employee that inherits Contact. When you do a Model2Db it automatically creates a PK field "ContactId" in the table Employee.

    Q 1) Can that name be EmployeeId? How can I do this, using fluent API?


    Q 2) I have a problem with associations when I have an association to Employee… I can only create an association to Contact.ContactId… I cannot change this in your OA model (example) and do not see a way to do this in Fluent API…

    Thanks in advance!

    Erik

  6. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 18 Jul 2012 Link to this post

    Hello Erik,

     1. Since you are using vertical inheritance you can redefine the contact id field mapping in each of the inheritors. So for example the mapping for the employee class should look like this:

    configuration.HasProperty(Function(x) x.ContactId).HasFieldName("_contactId").
                IsIdentity().ToColumn("EmployeeID")
    This way the context id field will result in a column named ContactID for the Contact class and EmployeeID for the Employee class.
    2. I was not fully able to understand the scenario here? Are you trying to create an association to just one of your inheritors or your goal is something different?

    Regards,
    Petar
    the Telerik team
    OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
  7. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 29 Jul 2012 Link to this post

    Thanks Petar,

    The first I understand; created something simulair. The second was some other problem. fixed that.

    How can i set the constraint name explicitly?

    .HasAssociation(Function(p) p.UserGroup).WithOpposite(Function(c) c.UserGroupUsers).HasConstraint(Function(p, c) p.UserGroupId = c.UserGroupId)

    Regards,

    Erik
  8. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 02 Aug 2012 Link to this post

    Hello Erik,

     Unfortunately, it is not possible to specify the name of the constraint with the Fluent API. If there is a constraint already in the database with any user-defined name, this constraint will be respected. If you create the schema from your fluent API definitions, OpenAccess will create a constraint for you with a name calculated by OpenAccess.

    Greetings,
    Zoran
    the Telerik team
    OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
Back to Top
DevCraft banner