Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
How to: Implement Inheritance in Reverse Mapped Database Model
Programmer's Guide > OpenAccess ORM Classic (Old API) > OpenAccess Tasks > Defining a Data Model > How to: Implement Inheritance in Reverse Mapped Database Model

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

Inheritance is one of the many features of OpenAccess which comes with the forward mapping approach. To implement inheritance in the reverse mapping scenario however, requires some skills that will be demonstrate in this topic. It is a common approach to go the “database schema first” design, when starting new application development.

For the purpose of this example a simple database schema is created that will be used in an inheritance model.

There is a common scenario where a Person and Employee tables are created and the Employee inherits from Person.

The first important thing to notice is that the identification field of the Employee must be the same as the one of the Person. That way a one-to-one relation is in effect that resembles inheritance between classes in the application’s codebase. The next step is to produce the persistent classes using the OpenAccess reverse-mapping wizard. First thing to do is to remove the reference to the Person class from the Employee class (which is automatically created by the wizard). This can be done by expanding the node of the derived class in the advanced tree view, and selecting the reference to the Person and than clicking the Remove button on the right.

After the removal of the reference click “Generate & Save Config”.
Since OpenAccess has already created the class model there are few steps that should be done to make the Inheritance model work as desired:

  •  Manually changing the Employee.OpenAccess.cs file:
  1. We should explicitly specify that Employee class inherits the Person class.
  2. The field that is used for identity of the Employee class ( the “employeeID” field) is no longer needed and should be deleted, because since we have inherited the Person class we already have its identification field available.
  3. "IdentityField" parameter from the Telerik.OpenAccess.Persistent() attribute of the Employee class should be removed as well because the field no longer exists in this class.
  • Manually changing the Employee.cs file:
  1. Only the property for the id field needs to be removed
  • Manually changing the App.config file:
  1. The value for the node of type ”extension” with key=”db-do-not-create-table” should be set to false. This is done so OpenAccess can edit our existing tables for the purpose of inheritance. This is usually the first child node of the “class” node in the “mappings” section and the step should be repeated for every class whose mapping info is described in the app.confg. In our case we perform this action for the Employee and Person.
  2. The “field” node for the employeeID field which we deleted from the .cs files should also be removed from the app.config.

 Now that the persistent model is set as required, we need to specify the strategy of inheritance used by OpenAccess. Start the Forward Mapping wizard, select the derived class and choose the vertical option in the “strategy” combo box (which is found in the “inheritance” group box). Vertical inheritance strategy means that separate tables will be used for the different classes in the hierarchy.

The last action that needs to be taken is to change the property “UpdateDatabse” of the project where the persistent classes are stored and than rebuild the application.

 This is how the database schema will look after OpenAccess did slightly modify it for the purpose of the example:

The only difference in the Employee table is that the identity field of it is also “PersonID” as it is in the table for the class it derives from. A “voa_class” column is also created in the Person table and that column is used by OpenAccess so it can track the class type of each record in it. That is of course, because a record in the Person table may be of type Person or Employee or any other class that eventually derive from it.

The following code is a small example that demonstrates the inheritance:

C# Copy Code
  static void Main(string[] args)
       {
           IObjectScope scope = Database.Get(
"DatabaseConnection1").GetObjectScope();
           Employee emp =
new Employee
           {
               FirstName =
"John",
               LastName =
"Smith",
               Ssn =
"1234567890",
               Position =
"Developer",
               Sale = 1000m
           };
           Person prs =
new Person { FirstName = "Robert", LastName = "Green", Ssn = "9876543210" };
           scope.Transaction.Begin();
           scope.Add(emp);
           scope.Add(prs);
           scope.Transaction.Commit();
           scope.Dispose();
       }
VB.NET Copy Code
   Shared Sub Main(ByVal args() As String)
   Dim scope As IObjectScope = Database.Get("DatabaseConnection1").GetObjectScope()
   Dim emp As Employee = New Employee With {.FirstName = "John", .LastName = "Smith", .Ssn = "1234567890", .Position = "Developer", .Sale = 1000D}
   Dim prs As Person = New Person With {.FirstName = "Robert", .LastName = "Green", .Ssn = "9876543210"}
   scope.Transaction.Begin()
   scope.Add(emp)
   scope.Add(prs)
   scope.Transaction.Commit()
   scope.Dispose()
   End Sub