Share via


Cómo definir un modelo con herencia de tabla por tipo (Entity Framework)

La herencia se puede implementar de varias formas en el Entity Data Model (EDM). El método de tabla por tipo (TPT, Table Per Type) emplea una tabla independiente del almacenamiento para mantener los datos de cada tipo en una jerarquía de herencia. En esta sección se incluyen los esquemas y la asignación de una jerarquía de herencia simple implementada en el escenario con una tabla por tipo.

En el EDM, el esquema conceptual para una jerarquía de herencia en el modelo de tabla por tipo incluye la asignación del atributo BaseType para las declaraciones de tipos derivados. Cada tipo EntityType derivado se declara por separado, pero la declaración de EntityContainer sólo incluye una declaración de EntitySet para el tipo base.

Las asociaciones en este escenario se implementan con el tipo base porque las definiciones de las asociaciones hacen referencia a las declaraciones de EntitySet. Un tipo derivado no tiene una declaración de EntitySet en EntityContainer.

Para implementar el esquema conceptual de la herencia de tabla por tipo

  1. Cree un proyecto de bibliotecas de clases.

  2. Haga clic en Agregar nuevo elemento y agregue un Entity Data Model de ADO.NET.

  3. Cuando el asistente aparece, cree un modelo vacío.

  4. Abra el archivo .edmx con un editor XML y agregue el segmento del lenguaje de definición de esquemas conceptuales (CSDL) del archivo.

  5. Implemente el esquema CSDL. Este esquema incluye declaraciones en un espacio de nombres denominado SchoolDataLib. La jerarquía de la herencia incluye un EntityType denominado Department, que es el tipo base, y tres entidades derivadas para el departamento comercial, de ingeniería y de música. Sólo el tipo base, Department, tiene una asignación del atributo Key. Los tipos derivados DeptBusiness, DeptEngineering, y DeptMusic incluyen la asignación del atributo BaseType. Las columnas Key de las tablas que representan los tipos derivados en el almacenamiento se asignan todas a la columna Key de la tabla que representa el tipo base.

  6. Implemente un AssociationType entre las entidades Department y Person. Esta asociación se usa en la implementación de la propiedad de navegación de un Administrator de School. El FK_Department_Administrator define una asociación que usan todos los tipos derivados del tipo Department en el que se declara. Todos los tipos derivados heredan la NavigationProperty que usa este AssociationType. El esquema de CSDL completo se muestra a continuación.

   <!-- CSDL content -->
      <Schema
          xmlns="https://schemas.microsoft.com/ado/2006/04/edm"
          Namespace="SchoolDataLib"
          Alias="Self">

        <EntityType Name="Department">
          <!--Base type table-per-type inheritance-->
          <Key>
            <PropertyRef Name="DepartmentID" />
          </Key>
          <Property Name="DepartmentID" Type="Int32" Nullable="false" />
          <Property Name="Name" Type="String" Nullable="false" />
          <Property Name="Budget" Type="Decimal" Nullable="false" />
          <Property Name="StartDate" Type="DateTime" Nullable="false" />
          <NavigationProperty Name="Administrator"
                              Relationship="SchoolDataLib.FK_Department_Administrator"
                              FromRole="Department" ToRole="Person" />
        </EntityType>

        <EntityType Name="DeptBusiness" BaseType="SchoolDataLib.Department">
          <Property Name="LegalBudget" Type="Decimal" Nullable="false" />
          <Property Name="AccountingBudget" Type="Decimal" Nullable="false" />
        </EntityType>

        <EntityType Name="DeptEngineering" BaseType="SchoolDataLib.Department">
          <Property Name="FiberOpticsBudget" Type="Decimal" Nullable="false" />
          <Property Name="LabBudget" Type="Decimal" Nullable="false" />
        </EntityType>

        <EntityType Name="DeptMusic" BaseType="SchoolDataLib.Department">
          <Property Name="TheaterBudget" Type="Decimal" Nullable="false" />
          <Property Name="InstrumentBudget" Type="Decimal" Nullable="false" />
        </EntityType>

        <Association Name="FK_Department_Administrator">
          <End Role="Person" Type="SchoolDataLib.Person" Multiplicity="0..1" />
          <End Role="Department" Type="SchoolDataLib.Department" Multiplicity="*" />
        </Association>

        <EntityType Name="Person">
          <!--Base type table-per-hierarchy inheritance-->
          <Key>
            <PropertyRef Name="PersonID" />
          </Key>
          <Property Name="PersonID" Type="Int32" Nullable="false" />
          <Property Name="FirstName" Type="String" Nullable="false" />
          <Property Name="LastName" Type="String" Nullable="false" />
          <NavigationProperty Name="Department"
                              Relationship="SchoolDataLib.FK_Department_Administrator"
                              FromRole="Person" ToRole="Department" />
        </EntityType>

        <EntityType Name="Student" BaseType="SchoolDataLib.Person">
          <Property Name="EnrollmentDate" Type="DateTime" />
        </EntityType>

        <EntityType Name="Instructor" BaseType="SchoolDataLib.Person">
          <Property Name="HireDate" Type="DateTime" />
        </EntityType>

        <EntityType Name="Administrator" BaseType="SchoolDataLib.Person">
          <Property Name="AdminDate" Type="DateTime" />
        </EntityType>

        <EntityContainer Name="SchoolDataLibContainer">
          <EntitySet Name="Departments" EntityType="SchoolDataLib.Department" />
          <EntitySet Name="People" EntityType="SchoolDataLib.Person" />

          <AssociationSet Name="FK_Department_Administrator"
                          Association="SchoolDataLib.FK_Department_Administrator">
            <End Role="Person" EntitySet="People" />
            <End Role="Department" EntitySet="Departments" />
          </AssociationSet>
        </EntityContainer>

      </Schema>

Para implementar el esquema de almacenamiento de la herencia de tabla por tipo

  1. Defina las tablas que contienen los datos para cada tipo de la jerarquía de herencia en el segmento del lenguaje de definición de esquemas de almacenamiento (SSDL) del archivo .edmx. A diferencia de las declaraciones de entidades en el esquema conceptual, las entidades de los tipos derivados en el modelo de almacenamiento tienen una propiedad Key.

  2. Use los tipos de datos del sistema de administración de bases de datos en lugar de los tipos de Common Language Runtime (CLR) que se emplean en el esquema conceptual para las propiedades en el esquema de almacenamiento.

  3. Use la sintaxis siguiente de SSDL para definir los metadatos de almacenamiento completos que se usan en este escenario de herencia.

<!-- SSDL content -->
      <Schema
          xmlns="https://schemas.microsoft.com/ado/2006/04/edm/ssdl"
          Namespace="SchoolDataLib.Target"
          Provider="System.Data.SqlClient"
          ProviderManifestToken="2005"
          Alias="Self">

        <EntityContainer Name="dbo">
          <EntitySet Name="Department" EntityType="SchoolDataLib.Target.Department" />
          <EntitySet Name="DeptBusiness" EntityType="SchoolDataLib.Target.DeptBusiness" />
          <EntitySet Name="DeptEngineering" EntityType="SchoolDataLib.Target.DeptEngineering" />
          <EntitySet Name="DeptMusic" EntityType="SchoolDataLib.Target.DeptMusic" />
          <EntitySet Name="Person" EntityType="SchoolDataLib.Target.Person" />

          <AssociationSet Name="FK_Department_Administrator"
                          Association="SchoolDataLib.Target.FK_Department_Administrator">
            <End Role="Person" EntitySet="Person" />
            <End Role="Department" EntitySet="Department" />
          </AssociationSet>

        </EntityContainer>

        <EntityType Name="Department">
          <Key>
            <PropertyRef Name="DepartmentID" />
          </Key>
          <Property Name="DepartmentID" Type="int" Nullable="false" />
          <Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="50" />
          <Property Name="Budget" Type="money" Nullable="false" />
          <Property Name="StartDate" Type="datetime" Nullable="false" />
          <Property Name="Administrator" Type="int" />
        </EntityType>

        <EntityType Name="DeptBusiness">
          <Key>
            <PropertyRef Name="BusinessDeptID" />
          </Key>
          <Property Name="BusinessDeptID" Type="int" Nullable="false" />
          <Property Name="LegalBudget" Type="money" Nullable="false" />
          <Property Name="AccountingBudget" Type="money" Nullable="false" />
        </EntityType>

        <EntityType Name="DeptEngineering">
          <Key>
            <PropertyRef Name="EngineeringDeptID" />
          </Key>
          <Property Name="EngineeringDeptID" Type="int" Nullable="false" />
          <Property Name="FiberOpticsBudget" Type="money" Nullable="false" />
          <Property Name="LabBudget" Type="money" Nullable="false" />
        </EntityType>

        <EntityType Name="DeptMusic">
          <Key>
            <PropertyRef Name="DeptMusicID" />
          </Key>
          <Property Name="DeptMusicID" Type="int" Nullable="false" />
          <Property Name="TheaterBudget" Type="money" Nullable="false" />
          <Property Name="InstrumentBudget" Type="money" Nullable="false" />
        </EntityType>

        <EntityType Name="Person">
          <Key>
            <PropertyRef Name="PersonID" />
          </Key>
          <Property Name="PersonID" Type="int" Nullable="false" />
          <Property Name="FirstName" Type="nvarchar" Nullable="false" MaxLength="50" />
          <Property Name="LastName" Type="nvarchar" Nullable="false" MaxLength="50" />
          <Property Name="HireDate" Type="datetime" />
          <Property Name="EnrollmentDate" Type="datetime" />
          <Property Name="AdminDate" Type="datetime" />
          <Property Name="PersonCategory" Type="smallint" Nullable="false" />
        </EntityType>

        <Association Name="FK_Department_Administrator">
          <End Role="Person" Type="SchoolDataLib.Target.Person" Multiplicity="0..1" />
          <End Role="Department" Type="SchoolDataLib.Target.Department" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="Person">
              <PropertyRef Name="PersonID" />
            </Principal>
            <Dependent Role="Department">
              <PropertyRef Name="Administrator" />
            </Dependent>
          </ReferentialConstraint>
        </Association>

      </Schema>

Para generar la base de datos mediante SQL Server Management Studio

  1. Use el siguiente script con SQL Server Management Studio para generar la base de datos que se usa en este ejemplo y en el ejemplo Cómo definir un modelo con herencia de tabla por jerarquía (Entity Framework).

  2. En el menú Archivo, seleccione Nuevo y haga clic en Consulta de motor de base de datos.

  3. Escriba el host local o el nombre de otra instancia de SQL Server en el cuadro de diálogo Conectarse al motor de base de datos y haga clic en Conectar.

  4. Pegue el siguiente script de Transact-SQL en la ventana de consulta y, a continuación, haga clic en Ejecutar.

USE [master]
GO

CREATE DATABASE [SchoolData] 
GO

USE [SchoolData]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DeptBusiness]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[DeptBusiness](
    [BusinessDeptID] [int] NOT NULL,
    [LegalBudget] [money] NOT NULL,
    [AccountingBudget] [money] NOT NULL,
 CONSTRAINT [PK_DeptBusiness] PRIMARY KEY CLUSTERED 
(
    [BusinessDeptID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DeptEngineering]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[DeptEngineering](
    [EngineeringDeptID] [int] NOT NULL,
    [FiberOpticsBudget] [money] NOT NULL,
    [LabBudget] [money] NOT NULL,
 CONSTRAINT [PK_DeptEngineering] PRIMARY KEY CLUSTERED 
(
    [EngineeringDeptID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DeptMusic]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[DeptMusic](
    [DeptMusicID] [int] NOT NULL,
    [TheaterBudget] [money] NOT NULL,
    [InstrumentBudget] [money] NOT NULL,
 CONSTRAINT [PK_DeptMusic] PRIMARY KEY CLUSTERED 
(
    [DeptMusicID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Course]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Course](
    [CourseID] [int] NOT NULL,
    [Title] [nvarchar](100) NOT NULL,
    [StartDate] [datetime] NOT NULL,
    [EndDate] [datetime] NOT NULL,
    [Credits] [int] NULL,
 CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED 
(
    [CourseID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Person]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Person](
    [PersonID] [int] NOT NULL,
    [FirstName] [nvarchar](50) NOT NULL,
    [LastName] [nvarchar](50) NOT NULL,
    [HireDate] [datetime] NULL,
    [EnrollmentDate] [datetime] NULL,
    [PersonCategory] [smallint] NOT NULL,
    [AdminDate] [datetime] NULL,
 CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED 
(
    [PersonID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Enrollment]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Enrollment](
    [EnrollmentID] [int] NOT NULL,
    [CourseID] [int] NOT NULL,
    [StudentID] [int] NOT NULL,
 CONSTRAINT [PK_Enrollment] PRIMARY KEY CLUSTERED 
(
    [EnrollmentID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CourseInstructor]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[CourseInstructor](
    [CourseInstructorID] [int] NOT NULL,
    [CourseID] [int] NOT NULL,
    [InstructorID] [int] NOT NULL,
 CONSTRAINT [PK_CourseInstructor] PRIMARY KEY CLUSTERED 
(
    [CourseInstructorID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Department]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Department](
    [DepartmentID] [int] NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [Budget] [money] NOT NULL,
    [StartDate] [datetime] NOT NULL,
    [Administrator] [int] NULL,
 CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED 
(
    [DepartmentID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Enrollment_Course]') AND parent_object_id = OBJECT_ID(N'[dbo].[Enrollment]'))
ALTER TABLE [dbo].[Enrollment]  WITH CHECK ADD  CONSTRAINT [FK_Enrollment_Course] FOREIGN KEY([CourseID])
REFERENCES [dbo].[Course] ([CourseID])
GO
ALTER TABLE [dbo].[Enrollment] CHECK CONSTRAINT [FK_Enrollment_Course]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Enrollment_Student]') AND parent_object_id = OBJECT_ID(N'[dbo].[Enrollment]'))
ALTER TABLE [dbo].[Enrollment]  WITH CHECK ADD  CONSTRAINT [FK_Enrollment_Student] FOREIGN KEY([StudentID])
REFERENCES [dbo].[Person] ([PersonID])
GO
ALTER TABLE [dbo].[Enrollment] CHECK CONSTRAINT [FK_Enrollment_Student]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_CourseInstructor_Course]') AND parent_object_id = OBJECT_ID(N'[dbo].[CourseInstructor]'))
ALTER TABLE [dbo].[CourseInstructor]  WITH CHECK ADD  CONSTRAINT [FK_CourseInstructor_Course] FOREIGN KEY([CourseID])
REFERENCES [dbo].[Course] ([CourseID])
GO
ALTER TABLE [dbo].[CourseInstructor] CHECK CONSTRAINT [FK_CourseInstructor_Course]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_CourseInstructor_Instructor]') AND parent_object_id = OBJECT_ID(N'[dbo].[CourseInstructor]'))
ALTER TABLE [dbo].[CourseInstructor]  WITH CHECK ADD  CONSTRAINT [FK_CourseInstructor_Instructor] FOREIGN KEY([InstructorID])
REFERENCES [dbo].[Person] ([PersonID])
GO
ALTER TABLE [dbo].[CourseInstructor] CHECK CONSTRAINT [FK_CourseInstructor_Instructor]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Department_Administrator]') AND parent_object_id = OBJECT_ID(N'[dbo].[Department]'))
ALTER TABLE [dbo].[Department]  WITH CHECK ADD  CONSTRAINT [FK_Department_Administrator] FOREIGN KEY([Administrator])
REFERENCES [dbo].[Person] ([PersonID])
GO
ALTER TABLE [dbo].[Department] CHECK CONSTRAINT [FK_Department_Administrator]

Para implementar la especificación de las asignaciones de la herencia de tabla por tipo

  1. Combine las etiquetas EntityTypeMapping para los tipos derivados debajo de la asignación de EntitySet para el tipo base. En el siguiente esquema del lenguaje de especificación de asignaciones (MSL), el EntitySet se denomina Departments según se define en el esquema conceptual.

  2. Use las etiquetas EntityTypeMapping debajo de la asignación EntitySet tanto para el tipo base como para los tipos derivados.

  3. Especifique cada tipo que se está asignando debajo de EntityTypeMapping por el atributo TypeName.

  4. Detrás de un atributo TableName coloque un TableMappingFragment.

  5. Asigne las propiedades de los tipos de entidad a las columnas especificadas en los metadatos de almacenamiento usando etiquetas ScalarProperty.

  6. Observe que las columnas de identidad de los tipos derivados se asignan todas a la propiedad de la entidad del tipo base, que en este caso se denomina DepartmentID.

  7. Use la siguiente sintaxis de MSL para asignar un EntitySet con la clase base Department.

  8. Especifique una EntityTypeMapping para cada uno de los tipos derivados.

<!-- C-S mapping content -->
<Mapping xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS"
               Space="C-S">
        <Alias Key="Model" Value="SchoolDataLib" />
        <Alias Key="Target" Value="SchoolDataLib.Target" />
        <EntityContainerMapping CdmEntityContainer="SchoolDataLibContainer" 
                  StorageEntityContainer="dbo">
          
          <!-- Mapping for table-per-type inheritance-->
          <EntitySetMapping Name="Departments">
            <EntityTypeMapping 
                     TypeName="IsTypeOf(SchoolDataLib.Department)">
              <MappingFragment StoreEntitySet="Department">
                <ScalarProperty 
                     Name="DepartmentID" ColumnName="DepartmentID" />
                <ScalarProperty Name="Name" ColumnName="Name" />
                <ScalarProperty Name="Budget" ColumnName="Budget" />
                <ScalarProperty 
                     Name="StartDate" ColumnName="StartDate" />
              </MappingFragment>
            </EntityTypeMapping>

            <EntityTypeMapping TypeName="SchoolDataLib.DeptBusiness">
              <MappingFragment StoreEntitySet="DeptBusiness">
                <ScalarProperty Name="DepartmentID" 
                                ColumnName="BusinessDeptID" />
                <ScalarProperty Name="AccountingBudget" 
                                ColumnName="AccountingBudget" />
                <ScalarProperty Name="LegalBudget" 
                                ColumnName="LegalBudget" />
              </MappingFragment>
            </EntityTypeMapping>

            <EntityTypeMapping TypeName="SchoolDataLib.DeptEngineering">
              <MappingFragment StoreEntitySet="DeptEngineering">
                <ScalarProperty Name="DepartmentID" 
                                ColumnName="EngineeringDeptID" />
                <ScalarProperty Name="FiberOpticsBudget" 
                                ColumnName="FiberOpticsBudget" />
                <ScalarProperty Name="LabBudget" 
                                ColumnName="LabBudget" />
              </MappingFragment>
            </EntityTypeMapping>

            <EntityTypeMapping TypeName="SchoolDataLib.DeptMusic">
              <MappingFragment StoreEntitySet="DeptMusic">
                <ScalarProperty Name="DepartmentID" 
                                ColumnName="DeptMusicID" />
                <ScalarProperty Name="TheaterBudget" 
                                ColumnName="TheaterBudget" />
                <ScalarProperty Name="InstrumentBudget" 
                                ColumnName="InstrumentBudget" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>

          <!--Mapping for table-per-hierarchy inheritance-->
          <EntitySetMapping Name="People">
            <EntityTypeMapping TypeName="SchoolDataLib.Person">
              <MappingFragment StoreEntitySet="Person">
                <ScalarProperty Name="PersonID" ColumnName="PersonID"/>
                <ScalarProperty Name="FirstName" ColumnName="FirstName"/>
                <ScalarProperty Name="LastName" ColumnName="LastName"/>
                <Condition ColumnName="PersonCategory" Value="0" />
              </MappingFragment>
            </EntityTypeMapping>

            <EntityTypeMapping TypeName="SchoolDataLib.Student">
              <MappingFragment StoreEntitySet="Person">
                <ScalarProperty Name="PersonID" ColumnName="PersonID" />
                <ScalarProperty Name="FirstName" ColumnName="FirstName" />
                <ScalarProperty Name="LastName" ColumnName="LastName" />
                <ScalarProperty 
                  Name="EnrollmentDate" ColumnName="EnrollmentDate" />
                <Condition ColumnName="PersonCategory" Value="1" />
              </MappingFragment>
            </EntityTypeMapping>

            <EntityTypeMapping TypeName="SchoolDataLib.Instructor">
              <MappingFragment StoreEntitySet="Person">
                <ScalarProperty Name="PersonID" ColumnName="PersonID" />
                <ScalarProperty Name="FirstName" ColumnName="FirstName" />
                <ScalarProperty Name="LastName" ColumnName="LastName" />
                <ScalarProperty Name="HireDate" ColumnName="HireDate" />
                <Condition ColumnName="PersonCategory" Value="2" />
              </MappingFragment>
            </EntityTypeMapping>

            <EntityTypeMapping TypeName="SchoolDataLib.Administrator">
              <MappingFragment StoreEntitySet="Person">
                <ScalarProperty Name="PersonID" ColumnName="PersonID" />
                <ScalarProperty Name="FirstName" ColumnName="FirstName" />
                <ScalarProperty Name="LastName" ColumnName="LastName" />
                <ScalarProperty Name="AdminDate" ColumnName="AdminDate" />
                <Condition ColumnName="PersonCategory" Value="3" />
              </MappingFragment>
            </EntityTypeMapping>

          </EntitySetMapping>


          <AssociationSetMapping Name="FK_Department_Administrator"
                    TypeName="SchoolDataLib.FK_Department_Administrator"
                    StoreEntitySet="Department">
            <EndProperty Name="Person">
              <ScalarProperty Name="PersonID" ColumnName="Administrator" />
            </EndProperty>
            <EndProperty Name="Department">
              <ScalarProperty Name="DepartmentID" ColumnName="DepartmentID" />
            </EndProperty>
            <Condition ColumnName="Administrator" IsNull="false" />
          </AssociationSetMapping>
        </EntityContainerMapping>
      </Mapping>

Vea también

Tareas

Cómo definir un modelo con herencia de tabla por tipo (Entity Framework)
Cómo agregar y modificar objetos con herencia de tabla por tipo (Entity Framework)
Cómo definir un modelo con herencia de tabla por jerarquía (Entity Framework)

Conceptos

Herencia (EDM)