The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.
.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

How to: Change Inheritance Mapping

XPO fully supports inheritance and polymorphism of persistent objects, that is you can retrieve a collection of base objects and then deal with the actual instances of concrete classes.


public class Person: XPObject {
    public string PhoneNumber = "";
}

public class Contact: Person {
    public string FirstName = "";
    public string LastName = "";
    public string Email = "";
    public Company Employer;
    // ...
}

public class Company: Person {
    public string Name = "";
    public string WebSite = "";
    // ...
}

In the code example above, the Contact and Company classes are based on the Person class. By default, XPO will store descendant-specific properties in separate tables and it will automatically build table joins to retrieve object data:

DefaultInheritance

If you want to store the properties of a descendant class in a base class table, you can change the inheritance mapping type to ParentTable. In this instance, XPO will store all properties from all the descendants in the same table.


public class Person: XPObject {
    public string PhoneNumber = "";
}

[MapInheritance(MapInheritanceType.ParentTable)]
public class Contact: Person {
    public string FirstName = "";
    public string LastName = "";
    public string Email = "";
    public Company Employer;
    // ...
}

[MapInheritance(MapInheritanceType.ParentTable)]
public class Company : Person {
    public string Name = "";
    public string WebSite = "";
    // ...
}

In the code example above, XPO will use a single table to store the inheritance hierarchy:

ParentTableInheriance

You can specify different inheritance mapping types for members of the same hierarchy. This technique gives you a great degree of control over data store layout in your database.

For developers working with persistent objects, both types of inheritance mapping look similar - it does not affect object behavior in any way. The only reason you will want to deal with the inheritance mapping type attribute is to fine-tune the database object store, segmentation and performance.

Note

By making the base class non-persistent you can implement inheritance mapping with which all the attributes, including inherited, are stored in the table which corresponds to a descendant class. The only exception of this implementation is that you cannot reference the base class in other persistent objects.

See Also