Udostępnij za pośrednictwem


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

This topic describes how to manually create a conceptual model that has a table-per-type inheritance hierarchy. Table-per-type inheritance uses a separate table in the database to maintain data for non-inherited properties and key properties for each type in the inheritance hierarchy.

Note

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-Type.

The basic steps for manually defining a model with table-per-type 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. 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. For more information, see EntitySetMapping 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 School.ssdl.

    <?xml version="1.0" encoding="utf-8" ?>
    <Schema Namespace="SchoolModel.Store" Alias="Self" Provider="System.Data.SqlClient"
                  ProviderManifestToken="2008"
                  xmlns:store="https://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
                  xmlns="https://schemas.microsoft.com/ado/2009/02/edm/ssdl">
      <EntityContainer Name="SchoolModelStoreContainer">
        <EntitySet Name="Course" EntityType="SchoolModel.Store.Course"
                   store:Type="Tables" Schema="dbo" />
        <EntitySet Name="OnlineCourse" EntityType="SchoolModel.Store.OnlineCourse"
                   store:Type="Tables" Schema="dbo" />
        <EntitySet Name="OnsiteCourse" EntityType="SchoolModel.Store.OnsiteCourse"
                   store:Type="Tables" Schema="dbo" />
      </EntityContainer>
      <EntityType Name="Course">
        <Key>
          <PropertyRef Name="CourseID" />
        </Key>
        <Property Name="CourseID" Type="int" Nullable="false" />
        <Property Name="Title" Type="nvarchar" Nullable="false" MaxLength="100" />
        <Property Name="Credits" Type="int" Nullable="false" />
        <Property Name="DepartmentID" Type="int" Nullable="false" />
      </EntityType>
      <EntityType Name="OnlineCourse">
        <Key>
          <PropertyRef Name="CourseID" />
        </Key>
        <Property Name="CourseID" Type="int" Nullable="false" />
        <Property Name="URL" Type="nvarchar" Nullable="false" MaxLength="100" />
      </EntityType>
      <EntityType Name="OnsiteCourse">
        <Key>
          <PropertyRef Name="CourseID" />
        </Key>
        <Property Name="CourseID" Type="int" Nullable="false" />
        <Property Name="Location" Type="nvarchar" Nullable="false" MaxLength="50" />
        <Property Name="Days" Type="nvarchar" Nullable="false" MaxLength="50" />
        <Property Name="Time" Type="smalldatetime" Nullable="false" />
      </EntityType>
    </Schema>
    

To create the conceptual model

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

    • Only one entity set, Courses, is defined for the three entity types: Course, OnlineCourse, and OnsiteCourse.

    • The OnlineCourse and OnsiteCourse entity types are derived types as indicated by the BaseType attribute in their definitions.

    • The properties defined for the OnlineCourse and OnsiteCourse entity types are only non-inherited properties.

    <?xml version="1.0" encoding="utf-8" ?>
    <Schema Namespace="SchoolModel" Alias="Self"
                  xmlns:annotation="https://schemas.microsoft.com/ado/2009/02/edm/annotation"
                  xmlns="https://schemas.microsoft.com/ado/2008/09/edm">
      <EntityContainer Name="SchoolEntities" annotation:LazyLoadingEnabled="true">
        <EntitySet Name="Courses" EntityType="SchoolModel.Course" />
      </EntityContainer>
      <EntityType Name="Course">
        <Key>
          <PropertyRef Name="CourseID" />
        </Key>
        <Property Name="CourseID" Type="Int32" Nullable="false" />
        <Property Name="Title" Type="String" Nullable="false"
                  MaxLength="100" Unicode="true" FixedLength="false" />
        <Property Name="Credits" Type="Int32" Nullable="false" />
        <Property Name="DepartmentID" Type="Int32" Nullable="false" />
      </EntityType>
      <EntityType Name="OnlineCourse" BaseType="SchoolModel.Course">
        <Property Name="URL" Type="String" Nullable="false"
                  MaxLength="100" Unicode="true" FixedLength="false" />
      </EntityType>
      <EntityType Name="OnsiteCourse" BaseType="SchoolModel.Course">
        <Property Name="Location" Type="String" Nullable="false"
                  MaxLength="50" Unicode="true" FixedLength="false" />
        <Property Name="Days" Type="String" Nullable="false"
                  MaxLength="50" Unicode="true" FixedLength="false" />
        <Property Name="Time" Type="DateTime" Nullable="false" />
      </EntityType>
    </Schema>
    

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

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

    • The mapping for the Course, OnlineCourse, and OnsiteCourse entity types is defined in the same EntitySetMapping element.

    • The inherited CourseID properties for OnlineCourse and OnsiteCourse are mapped to the corresponding columns in the underlying database tables.

    • For each entity type mapping, the IsTypeOf syntax is used to indicate the entity type that is being mapped.

    <?xml version="1.0" encoding="utf-8" ?>
    <Mapping Space="C-S" xmlns="https://schemas.microsoft.com/ado/2008/09/mapping/cs">
      <EntityContainerMapping StorageEntityContainer="SchoolModelStoreContainer"
                              CdmEntityContainer="SchoolEntities">
        <EntitySetMapping Name="Courses">
          <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Course)">
            <MappingFragment StoreEntitySet="Course">
              <ScalarProperty Name="CourseID" ColumnName="CourseID" />
              <ScalarProperty Name="Title" ColumnName="Title" />
              <ScalarProperty Name="Credits" ColumnName="Credits" />
              <ScalarProperty Name="DepartmentID" ColumnName="DepartmentID" />
            </MappingFragment>
          </EntityTypeMapping>
          <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.OnlineCourse)">
            <MappingFragment StoreEntitySet="OnlineCourse">
              <ScalarProperty Name="CourseID" ColumnName="CourseID" />
              <ScalarProperty Name="URL" ColumnName="URL" />
            </MappingFragment>
          </EntityTypeMapping>
          <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.OnsiteCourse)">
            <MappingFragment StoreEntitySet="OnsiteCourse">
              <ScalarProperty Name="CourseID" ColumnName="CourseID" />
              <ScalarProperty Name="Location" ColumnName="Location" />
              <ScalarProperty Name="Days" ColumnName="Days" />
              <ScalarProperty Name="Time" ColumnName="Time" />
            </MappingFragment>
          </EntityTypeMapping>
        </EntitySetMapping>
      </EntityContainerMapping>
    </Mapping>
    

See Also

Tasks

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

Other Resources

CSDL, SSDL, and MSL Specifications
Entity Data Model: Inheritance
Defining Advanced Data Models