Condividi tramite


Procedura: definire un modello con ereditarietà tabella per tipo (Entity Framework)

L'ereditarietà può essere implementata in diversi modi in Entity Data Model (EDM). Il metodo tabella per tipo utilizza una tabella distinta nell'archivio per gestire dati per ogni tipo nella gerarchia di ereditarietà. In questa sezione sono inclusi gli schemi e il mapping per una gerarchia di ereditarietà semplice implementata nello scenario tabella per tipo.

In EDM lo schema concettuale per una gerarchia di ereditarietà nel modello tabella per tipo include l'assegnazione di attributi BaseType per le dichiarazioni dei tipi derivati. Ogni oggetto EntityType derivato viene dichiarato separatamente, ma la dichiarazione di EntityContainer include solo una dichiarazione di EntitySet per il tipo di base.

Le associazioni in questo scenario vengono implementate nel tipo di base, in quanto le definizioni delle associazioni fanno riferimento a dichiarazioni di EntitySet. Un tipo derivato non include alcuna dichiarazione di EntitySet in EntityContainer.

Per implementare lo schema concettuale per l'ereditarietà tabella per tipo

  1. Creare un progetto Libreria di classi.

  2. Fare clic su Aggiungi nuovo elemento, quindi aggiungere un modello ADO.NET Entity Data Model.

  3. Quando viene visualizzata la procedura guidata, creare un modello vuoto.

  4. Aprire il file con estensione edmx con un editor XML e cercare il segmento CSDL (Conceptual Schema Definition Language) del file.

  5. Implementare lo schema CSDL. Questo schema include dichiarazioni in uno spazio dei nomi denominato SchoolDataLib. La gerarchia di ereditarietà include un oggetto EntityType denominato Department, che rappresenta il tipo di base, e tre entità derivate per reparto commerciale, reparto tecnico e reparto musicale. Solo il tipo di base, Department, include un'assegnazione di attributi Key. I tipi derivati DeptBusiness, DeptEngineering e DeptMusic includono l'assegnazione di attributi BaseType. Le colonne Key delle tabelle che rappresentano i tipi derivati nell'archivio vengono tutte mappate alla colonna Key della tabella che rappresenta il tipo di base.

  6. Implementare un oggetto AssociationType tra le entità Department e Person. Questa associazione viene utilizzata nell'implementazione della proprietà di navigazione per un oggetto SchoolAdministrator. FK_Department_Administrator definisce un'associazione utilizzata da tutti i tipi derivati dal tipo Department in cui viene dichiarata. L'oggetto NavigationProperty che utilizza AssociationType viene ereditato da tutti i tipi derivati. Lo schema CSDL completo viene illustrato di seguito.

   <!-- 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>

Per implementare lo schema di archiviazione per l'ereditarietà tabella per tipo

  1. Definire le tabelle contenenti i dati per ogni tipo della gerarchia di ereditarietà nel segmento SSDL del file con estensione edmx. A differenza delle dichiarazioni di entità nello schema concettuale, le entità per i tipi derivati nel modello di archiviazione includono una proprietà Key.

  2. Utilizzare i tipi di dati del sistema di gestione di database anziché i tipi Common Language Runtime (CLR) utilizzati nello schema concettuale per le proprietà incluse nello schema di archiviazione.

  3. Utilizzare la sintassi seguente in SSDL per definire i metadati di archiviazione completi utilizzati in questo scenario di ereditarietà.

<!-- 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>

Per generare il database tramite SQL Server Management Studio

  1. Utilizzare lo script seguente con SQL Server Management Studio per generare il database utilizzato in questo esempio e nell'esempio dell'argomento Procedura: definire un modello con ereditarietà tabella per gerarchia (Entity Framework).

  2. Scegliere Nuovo dal menu File, quindi fare clic su Query del Motore di database.

  3. Nella finestra di dialogo Connetti al Motore di database digitare localhost o il nome di un'altra istanza di SQL Server, quindi fare clic su Connetti.

  4. Incollare lo script Transact-SQL seguente nella finestra di query, quindi fare clic su Esegui.

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]

Per implementare la specifica di mapping per l'ereditarietà tabella per tipo

  1. Combinare i tag EntityTypeMapping per i tipi derivati nel mapping EntitySet per il tipo di base. Nello schema Mapping Specification Language (MSL) seguente l'oggetto EntitySet è denominato Departments, come definito nello schema concettuale.

  2. Utilizzare i tag EntityTypeMapping nel mapping EntitySet sia per il tipo di base sia per i tipi derivati.

  3. Specificare ogni tipo mappato in EntityTypeMapping dall'attributo TypeName.

  4. Fare seguire a un attributo TableName un oggetto TableMappingFragment.

  5. Eseguire il mapping delle proprietà dei tipi di entità alle colonne specificate nei metadati di archiviazione utilizzando tag ScalarProperty.

  6. Si noti che le colonne Identity dei tipi derivati vengono tutte mappate alla proprietà Identity del tipo di base, in questo caso denominata DepartmentID.

  7. Utilizzare la sintassi MSL per eseguire il mapping di un oggetto EntitySet utilizzando la classe base Department.

  8. Specificare un oggetto EntityTypeMapping per ciascun tipo derivato.

<!-- 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>

Vedere anche

Attività

Procedura: definire un modello con ereditarietà tabella per tipo (Entity Framework)
Procedura: aggiungere e modificare oggetti con ereditarietà tabella per tipo (Entity Framework)
Procedura: definire un modello con ereditarietà tabella per gerarchia (Entity Framework)

Concetti

Ereditarietà (EDM)