Condividi tramite


How to: Create and Execute Object Queries using Table-per-Hierarchy Inheritance (Entity Framework)

Inheritance in the Entity Data Model (EDM) provides the same advantages of inheritance that apply in object-oriented programming. Instances of types in the hierarchy can be operated on together or as instances of one of the subtypes. All subtypes inherit the properties of the base type and additional properties implemented by the subtypes can be accessed as needed.

To create the project using Table-Per-Hierarchy inheritance

  1. Create a console application project and add references to System.Data.Entity and System.Runtime.Serialization.

  2. Add a reference to the dll built from the inheritance model in the topic How to: Define a Model with Table-per-Hierarchy Inheritance (Entity Framework).

  3. Add the schemas from the topic How to: Define a Model with Table-per-Hierarchy Inheritance (Entity Framework) to the same folder as the executable.

  4. Add an application configuration file with content in the following example.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings><add name="SchoolDataLibContainer" 
   connectionString=
   "metadata=res://*/SchoolDataLib.csdl|
   res://*/SchoolDataLib.ssdl|
   res://*/SchoolDataLib.msl;provider=System.Data.SqlClient;
   provider connection string=&quot;
   Data Source=localhost;
   Initial Catalog=SchoolData;Integrated Security=True;
   MultipleActiveResultSets=True&quot;" 
   providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

Example

The following code displays all instances of types in the hierarchy in a foreach loop that enumerates instances of types from the People collection on the SchoolDataEntities ObjectContext.

The type of each instance is displayed by a call to the GetType method.

The second foreach loop uses the OfType filter on the People collection to return only instances of type Instructor.

Only the Instructor type contains the HireDate property. The code displays HireDate with first and last names of the instructors returned by the query.

Option Strict On
Option Explicit On
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports SchoolDataLib

Module Module1

    Sub Main()
        Try
            Using objectContext As SchoolDataLibContainer = New SchoolDataLibContainer()
                ' Display departments and administrators.
                For Each dept As Department In objectContext.Departments
                    Console.WriteLine("School: {0} Budget: {1}", _
                        dept.Name, dept.Budget)

                    ' Load associated person reference.
                    dept.AdministratorReference.Load()
                    If dept.Administrator IsNot Nothing Then
                        Console.WriteLine("Administrator: {0} {1}", _
                            dept.Administrator.FirstName, _
                            dept.Administrator.LastName)
                    End If

                    For Each student As Student In objectContext.People.OfType(Of Student)()
                        Console.WriteLine("Student: {0} {1}", student.FirstName, _
                            student.LastName)
                    Next

                    For Each person As Person In objectContext.People
                        Console.WriteLine("{0} Name: {1}", _
                            person.GetType().Name, person.LastName)
                    Next

                    For Each instructor In objectContext.People. _
                            OfType(Of Instructor)()
                        Console.WriteLine("Instructor: {0} {1} Hire Date: {2}", _
                            instructor.FirstName, _
                            instructor.LastName, _
                            instructor.HireDate)
                    Next

                Next
            End Using

        Catch ex As System.Data.MappingException
            Console.WriteLine(ex.ToString())
        Catch ex As System.Data.UpdateException
            Console.WriteLine(ex.ToString())
        End Try
    End Sub

End Module
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchoolDataLib;
using System.Data.Objects;

namespace SchoolDataClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (SchoolDataLibContainer objectContext =
                                      new SchoolDataLibContainer ())
                {
                    // Display departments and administrators.
                    foreach (Department dept in objectContext.Departments)
                    {
                        Console.WriteLine("School: {0} Budget: {1}",
                            dept.Name,
                            dept.Budget);

                        // Load associated person reference.
                        dept.AdministratorReference.Load();
                        if (null != dept.Administrator)
                            Console.WriteLine("Administrator: {0} {1}",
                                         dept.Administrator.FirstName,
                                         dept.Administrator.LastName);
                    }

                    foreach (Student student in
                        objectContext.People.OfType<Student>())
                    {
                        Console.WriteLine("Student: {0} {1}", student.FirstName,
                            student.LastName);

                    }

                    
                    foreach (Person person in objectContext.People)
                        Console.WriteLine("{0} Name: {1}",
                            person.GetType().Name,
                            person.LastName);

                    foreach (Instructor instructor in
                        objectContext.People.OfType<Instructor>())
                        Console.WriteLine("Instructor: {0} {1} Hire Date: {2}",
                            instructor.FirstName,
                            instructor.LastName,
                            instructor.HireDate);

                    objectContext.Connection.Close();
                }
            }

            catch (System.Data.MappingException e)
            {
                Console.WriteLine(e.ToString());
            }
            catch (System.Data.UpdateException e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

See Also

Tasks

How to: Define a Model with Table-per-Hierarchy Inheritance (Entity Framework)

Concepts

Inheritance (EDM)