HOW TO:使用每個階層的資料表繼承來建立和執行物件查詢 (Entity Framework)

實體資料模型 (EDM) 中的繼承提供了物件導向程式設計中所適用的相同繼承好處。此階層中的型別執行個體可以一起運作,或是當做其中一個子型別的執行個體。所有子型別都會繼承基底類型的屬性,而且可以視需要存取子型別所實作的其他屬性。

若要使用每個階層的資料表繼承來建立專案

  1. 建立主控台應用程式專案,並加入 System.Data.EntitySystem.Runtime.Serialization 的參考。

  2. 加入從 HOW TO:使用每個階層的資料表繼承來定義模型 (Entity Framework) 主題中之繼承模型建置而來之 dll 的參考。

  3. 將主題 HOW TO:使用每個階層的資料表繼承來定義模型 (Entity Framework) 中的結構描述加入至與可執行檔相同的資料夾。

  4. 加入具有底下範例內容的應用程式組態檔。

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

範例

下列程式碼會在 foreach 迴圈中顯示階層內具型別的所有執行個體,該迴圈會列舉 SchoolDataEntitiesObjectContextPeople 集合的具型別執行個體。

每一個執行個體的型別都是由 GetType 方法的呼叫所顯示。

第二個 foreach 迴圈會使用 People 集合上的 OfType 篩選,傳回只限 Instructor 型別的執行個體。

只有 Instructor 型別包含 HireDate 屬性。此程式碼會顯示 HireDate,包含查詢傳回的講師姓名。

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());
            }
        }
    }
}

另請參閱

工作

HOW TO:使用每個階層的資料表繼承來定義模型 (Entity Framework)

概念

繼承 (EDM)