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

How to: Convert Flat 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 flat inheritance hierarchies. The purpose of this topic is to introduce you to the most specific remarks about the inheritance conversion. Flat inheritance means that all levels in the inheritance hierarchy are stored in the same table, and a discriminator column specifies which rows for which classes are.

Suppose you have the following Entity Framework model implementing Flat Inheritance.

A condition is set that when the HireDate column on the Person table is not null, it should be mapped to Instructor entity. Respectively, if the EntollmentDate is not null, the row will be mapped to a Student entity. The implementation of Flat inheritance in Telerik Data Access is different and you should perform some additional actions on the converted domain model.

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 Student and Instructor is set to Flat.

The converted domain model will not work runtime as it is. The reason is in the different implementation of Flat inheritance in Telerik Data Access. For example, in Telerik Data Access you cannot use conditions to specify which row to which entity will be mapped, i.e. you cannot say "if the HireDate is not null then map this row to Instructor, etc". 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 your child classes (e.g. Student and Instructor) to voa_class. Also, you need to specify a Discriminator Value. The Discriminator Column is of type Int, so the Discriminator Value should be also integer. For example, set 1 for Student and 2 for Instructor.

  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 [Person] ADD [voa_class] INT NULL
    GO
    UPDATE [Person] SET [voa_class] = 0
    GO
    ALTER TABLE [Person] ALTER COLUMN [voa_class] INT NOT NULL
    GO
    

    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 according to the previous Discriminator Columns/Values. For example, the original EF data model uses HireDate and EnrollmentDate to decide which rows for which classes are:

    -- add column for field <internal-class-id>
    ALTER TABLE [Person] ADD [voa_class] INT NULL
    GO
    UPDATE [Person] SET [voa_class] = 0
    GO
    UPDATE [Person] SET [voa_class] = 1 WHERE HireDate IS NOT NULL
    GO
    UPDATE [Person] SET [voa_class] = 0 WHERE EnrollmentDate IS NOT NULL
    GO
    ALTER TABLE [Person] ALTER COLUMN [voa_class] INT NOT NULL
    GO