How to: Model Vertical Inheritance with the Visual Designer

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

The simplest, most straight-forward approach to model inheritance is to use vertical mapping. With vertical mapping each class has its own table containing only its fields. That strategy is also known as "table-per-concrete-class" model. Each concrete persistent class in the domain model is persisted to its own table. For example, a Customer object is stored in the Customers table.

This topic demonstrates how to implement vertical inheritance mapping with the Telerik Data Access Visual Designer.

  1. Create a new console application in Visual Studio.
  2. For this walkthrough, you will need to create a new empty database named AnimalKingdom on your server.
  3. Add a new Telerik Data Access Domain Model to your project. Name the domain model AnimalKingdomDomainModel. In the Select Domain Model Type Dialog, select the Empty Domain Model option.

  4. Click Finish to generate an empty model. The next several steps demonstrate how to create a sample inheritance hierarchy in Visual Designer.

  5. If the generated domain model is not opened in Visual Designer, double click it in the Solution Explorer to open it.
  6. Next, you have to declare several entities in Visual Designer. To add a new entity in the design surface, open the Toolbox and drag the Domain Class item onto the design surface. To add a new property to a domain class, right-click the target domain class, point to Add, and then click Property. Another way to add a new property is to right-click the Properties section in the domain class, and then select Add new Property. Add four entities with the following characteristics:

    1. Animal - this will be the base class in the hierarchy. It exposes the following properties:
      1. AnimalId - this is the primary key for the entity. The type of this property is Int32. The Identity property of AnimalId is set to True.
      2. Name - the type of this property is String.
    2. Dog - derives from Animal. You don't have to specify a primary key for the Dog class, because it is a subclass in a vertical inheritance hierarchy. The class exposes the following properties:
      1. Age - the type of this property is Int32.
    3. Cat - derives from Animal. You don't have to specify a primary key for the Cat class, because it is a subclass in a flat inheritance hierarchy. The class exposes the following properties:
      1. LivesLeft - the type of this property is Int32.
    4. WienerDog - derives from Dog. You don't have to specify a primary key for the WienerDog class, because it is a subclass in a vertical inheritance hierarchy. The class exposes the following properties:
      1. Length - the type of this property is Decimal.
  7. Finally, your model should look like the snapshot below.

  8. The next step is to define inheritance associations. For that purpose, open the Toolbox pane again. Select the Inheritance item.

  9. Select the objects to be included in the inheritance by clicking the derived entity first (Dog or Cat) on the design surface and then clicking the base entity on the design surface (Animal). Do the same for the WienerDog entity. However, it must derive from the Dog entity.
  10. When you specify all inheritance relationships, your model should look like the snapshot below:

  11. When modelling a vertical inheritance, a primary key is specified only for the root class of the hierarchy. In this demo, this is the Animal class. Note that any of the derived types does not have a primary key.

  12. Next, you have to change the inheritance strategy for all subclasses. Select the Cat entity in Visual Designer and open the Mapping Details Editor. Navigate to the Inheritance Mappings section. Change the Inheritance Strategy to Vertical.

  13. Select the Dog entity in Visual Designer and open the Mapping Details Editor. Navigate to the Inheritance Mappings section. Change the Inheritance Strategy to Vertical.

  14. Select the WienerDog entity in Visual Designer and open the Mapping Details Editor. Navigate to the Inheritance Mappings section. Change the Inheritance Strategy to Vertical.

  15. Finally, select the Animal entity in Visual Designer and open the Mapping Details Editor. Navigate to the Inheritance Mappings section. The Inheritance Strategy should be set to Default.

  16. The next step is to map all entities to tables. Select the Animal entity in Visual Designer and open the Mapping Details Editor (View->Other Windows->Entity Diagrams Details Editor). In the Table Mappings section, check the Use Default Mapping option.

    Do the same for the Cat, Dog and WienerDog entities.

  17. The only step left is to update the database with the changes made so far to the model. To do so, rebuild the project and start the Update Database from Model Wizard (right-click on an empty area in Visual Designer and choose Update Database from Model). In the Setup Database Connection dialog specify a connection string to the AnimalKingdom database which was created on step 2. On the second page of the wizard, you need to select the Create Database strategy. On the Summary page (the third page), you could review the generated script and execute it. Execute the script. For more information about the Update Database from Model wizard, take a look at How to: Update an Existing Database Schema.
  18. Spend some time to analyze the generated script. The generated script should be similar to the SQL script listed below:

    -- VerticalInheritanceDemo.Animal
    CREATE TABLE [animal] (
       [animal_id] INT NOT NULL,               -- animalId
       [nme] varchar(255) NULL,                -- name
       CONSTRAINT [pk_animal] PRIMARY KEY ([animal_id])
    )
    GO
    -- VerticalInheritanceDemo.Cat
    CREATE TABLE [cat] (
       [animal_id] INT NOT NULL,
       [lives_left] INT NULL,                  -- Cat.livesLeft
       CONSTRAINT [pk_cat] PRIMARY KEY ([animal_id])
    )
    GO
    -- VerticalInheritanceDemo.Dog
    CREATE TABLE [dog] (
       [animal_id] INT NOT NULL,
       [age] INT NULL,                         -- Dog.age
       CONSTRAINT [pk_dog] PRIMARY KEY ([animal_id])
    )
    GO
    -- VerticalInheritanceDemo.WienerDog
    CREATE TABLE [wiener_dog] (
       [animal_id] INT NOT NULL,
       [lngth] NUMERIC(20,10) NULL,            -- WienerDog.length
       CONSTRAINT [pk_wiener_dog] PRIMARY KEY ([animal_id])
    )
    GO
    ALTER TABLE [cat] ADD CONSTRAINT [ref_cat_animal] FOREIGN KEY ([animal_id]) 
    REFERENCES [animal]([animal_id])
    GO
    ALTER TABLE [dog] ADD CONSTRAINT [ref_dog_animal] FOREIGN KEY ([animal_id]) 
    REFERENCES [animal]([animal_id])
    GO
    ALTER TABLE [wiener_dog] ADD CONSTRAINT [ref_wiener_dog_animal] FOREIGN KEY ([animal_id]) 
    REFERENCES [animal]([animal_id])
    GO
    
  19. Execute the generated script. Now you are ready to start working with your domain model.

    using (EntitiesModel dbContext = new EntitiesModel())
    {
       Cat cat = new Cat();
       cat.AnimalId = 2;
       cat.LivesLeft = 12;
       cat.Name = "Tomas";
       dbContext.Add(cat);
       dbContext.SaveChanges();
       foreach (Animal animal in dbContext.Animals)
       {
           Console.WriteLine(animal.AnimalId);
       }
    }
    
    Using dbContext As New EntitiesModel()
     Dim _cat As New Cat()
     _cat.AnimalId = 2
     _cat.LivesLeft = 12
     _cat.Name = "Tomas"
     dbContext.Add(_cat)
     dbContext.SaveChanges()
     For Each _animal As Animal In dbContext.Animals
      Console.WriteLine(_animal.AnimalId)
     Next _animal
    End Using
    

See Also