How to: Define a Model with Table-per-Hierarchy Inheritance

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

This topic describes how to manually create a conceptual model that has table-per-hierarchy inheritance. Table-per-hierarchy inheritance uses one database table to maintain data for all of the entity types in an inheritance hierarchy.

The recommended way to define a model with table-per-type inheritance is by using the ADO.NET Entity Data Model Tools. For more information, see Walkthrough: Mapping Inheritance - Table-per-Hierarchy.

The basic steps for manually defining a model with table-per-hierarchy inheritance are as follows:

  1. Define one entity set in the conceptual model that will contain the base entity type and the derived types. For more information, see EntitySet Element (CSDL).

  2. Define derived entity types in the conceptual model by using the BaseType attribute and define only non-inherited properties on the derived types. For more information, see EntityType Element (CSDL).

  3. Choose a column from the underlying database table whose value will be used to discriminate between the base type and derived types. For example, if an Employee table has an integer-valued EmployeeType column, the value of this column could be used to discriminate when an Employee base entity type is one of the following derived entity types: HourlyEmployee or SalariedEmployee. For more information, see the following example.

    The discriminator column is mapped as part of a condition, so there can be no corresponding property on any entity types in the hierarchy. The exception to this rule is when the condition uses an Is Null or Is Not Null comparison. In this case, the discriminator column can have a corresponding property on an entity type.

    If the discriminator column can have more than two values (an integer type, for example), the column must be nullable or have a default value. This ensures that when a new type is created and saved to the database, the column can be null or has some value.

    Conditions must be used to map each derived type in the hierarchy, and conditions may be used to map the base type. If the base type is abstract, no mapping or condition is allowed.

  4. Map the base entity type and derived types in the same EntitySetMapping element in the mapping specification language (MSL). Map inherited properties to table columns where appropriate. Use the IsTypeOf syntax when setting the value of the TypeName attribute on derived types. Use a mapping condition to discriminate between types in the hierarchy. For more information, see EntitySetMapping Element (MSL) and Condition Element (MSL).

The following example assumes that you have installed the School sample database and that you have manually configured your project to use the Entity Framework. For more information, see Creating the School Sample Database and Configuring the Entity Framework.

To create the storage model

  1. Add the following XML file to your project and name it AdventureWorks.ssdl.

To create the conceptual model

  1. Add the following XML file to your project and name it AdventureWorks.csdl. Note the following:

    • Only one entity set, Products, is defined for the two entity types Product and DiscontinuedProduct.

    • The DiscontinuedProduct entity type is a derived type as indicated by the BaseType attribute in its definition.

    • The properties defined on the DiscontinuedProduct entity type are only non-inherited properties.

    • The MakeFlag column in the storage model (see the AdventureWorks.ssdl file above) does not appear as a property on the base type or the derived type. The value of this column will be used to discriminate between the types in the hierarchy as is mapped as part of a condition (see the AdventureWorks.msl file below).

    If the column is used in an Is Null or Is Not Null condition, then the corresponding property can appear on an entity type.

To define the mapping between the conceptual model and the storage model

  1. Add the following XML file to your project and name it AdventureWorks.msl. Note the following:

    • The mapping for the Product and DiscontinuedProduct entity types are defined in the same EntitySetMapping element.

    • The inherited properties for the DiscontinuedProduct are mapped to the corresponding columns in the underlying database table.

    • The IsTypeOf syntax is used to indicate the type of the derived DiscontinuedProduct type.

    • The discriminator column, MakeFlag, is mapped in a Condition element for each entity type in the hierarchy.

See Also