Data Access has been discontinued. Please refer to this page for more information.

How to: Convert Vertical Inheritance

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 Entity Framework Conversion Wizard allows you to convert Entity Framework models with vertical inheritance hierarchies. The purpose of this topic is to introduce you to the most specific remarks about the inheritance conversion. Vertical inheritance means that each class has its own table containing only its fields. That strategy is also known as "table-per-concrete-class" model.

Suppose you have the following Entity Framework model implementing Vertical Inheritance. Each entity is represented with a table on the server.

The Entity Framework Conversion Wizard will convert the original data model as it is:

If there aren't inheritance relations in the Visual Designer, you will need to add them manually by using the Inheritance item from the Toolbox.

Next, open the Mapping Details Editor and ensure that the Inheritance Strategy from OnsiteCourse and OnlineCourse is set to Vertical.

The converted domain model will not work runtime as it is. The reason is in the different implementation of Vertical inheritance in Telerik Data Access. In Telerik Data Access conditions are applied via a "discriminator" column. In other words, matching each of the table rows to a certain class is done based on a value provided in a specific column which is configurable. Here are the basic steps you need to perform:

  1. Add a new column named voa_class in the root table in the domain model. This is the discriminator column. Later, you will migrate your database to the latest domain model state by using the Update Database from Model Wizard. Right click the root class in the Visual Designer and select Edit Table.... This will open the Table Editor. Create the voa_class column (not null, int32) and click OK.

  2. Go back to the Mapping Details Editor. Set the Discriminator Column for the root class in the inheritance hierarchy to <Default>. Also, you need to specify a Discriminator Value. The Discriminator Column is of type Int, so the Discriminator Value should be also integer. Set Discriminator Value for each class that participates in the hierarchy, for e.g. set 0 for Course (the root class), 1 for OnlineCourse, 2 for OnsiteCourse, etc.

  3. At this point, your domain model is fully configured. The next step is to migrate your database to the latest model state. You will need to use the Update Database from Model Wizard. For a complete tutorial, please refer to How to: Update an Existing Database Schema. The end script should be similar to this:

    -- add column for field <internal-class-id>
    ALTER TABLE [Course] ADD [voa_class] INT NULL
    GO
    UPDATE [Course] SET [voa_class] = 0
    GO
    ALTER TABLE [Course] ALTER COLUMN [voa_class] INT NOT NULL
    GO
    

    The voa_class column will be added to the base Course class. By default the voa_class column will be set to 0. However, you will need to modify the script so it updates the voa_class column depending on which rows for which entities are. For example, you need to set the voa_class column for all rows that will be mapped to OnlineCourse to 2. You will need to use the OnlineCourse -> Course foreign key. For example:

    -- add column for field <internal-class-id>
    ALTER TABLE [Course] ADD [voa_class] INT NULL
    GO
    UPDATE [Course] SET [voa_class] = 0
    GO
    UPDATE Course SET voa_class = 2 WHERE CourseID IN (select CourseID from OnlineCourse)
    GO
    UPDATE Course SET voa_class = 1 WHERE CourseID IN (select CourseID from OnsiteCourse)
    GO
    ALTER TABLE [Course] ALTER COLUMN [voa_class] INT NOT NULL
    GO