如何:执行多态查询How to: Execute a Polymorphic Query

本主题说明如何 Entity SQLEntity SQL 使用 OFTYPE 运算符执行多态查询。This topic shows how to execute a polymorphic Entity SQLEntity SQL query using the OFTYPE operator.

运行本示例中的代码To run the code in this example

  1. School 模型 添加到项目中,并将项目配置为使用实体框架。Add the School Model to your project and configure your project to use the Entity Framework. 有关详细信息,请参阅 如何:使用实体数据模型向导For more information, see How to: Use the Entity Data Model Wizard.

  2. 在应用程序的代码页中,添加以下 using 语句(在 Visual Basic 中为 Imports):In the code page for your application, add the following using statements (Imports in Visual Basic):

    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Data.Common;
    using System.Data;
    using System.IO;
    using System.Data.SqlClient;
    using System.Data.EntityClient;
    using System.Data.Metadata.Edm;
    
    Imports System.Collections.Generic
    Imports System.Collections
    Imports System.Data.Common
    Imports System.Data
    Imports System.IO
    Imports System.Data.SqlClient
    Imports System.Data.EntityClient
    Imports System.Data.Metadata.Edm
    
    
  3. 按照 演练:映射继承-每个层次结构一个表中的步骤修改概念模型,使其具有每个层次结构一个表继承。Modify the conceptual model to have a table-per-hierarchy inheritance by following the steps in Walkthrough: Mapping Inheritance - Table-per-Hierarchy.

示例Example

下面的示例使用 OFTYPE 运算符从 OnsiteCourses 的集合中获取和显示仅包含 Courses 的集合。The following example uses an OFTYPE operator to get and display a collection of only OnsiteCourses from a collection of Courses.

using (EntityConnection conn = new EntityConnection("name=SchoolEntities"))
{
    conn.Open();
    // Create a query that specifies to
    // get a collection of only OnsiteCourses.

    string esqlQuery = @"SELECT VAlUE onsiteCourse FROM
        OFTYPE(SchoolEntities.Courses, SchoolModel.OnsiteCourse)
        AS onsiteCourse";
    using (EntityCommand cmd = new EntityCommand(esqlQuery, conn))
    {
        // Execute the command.
        using (DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
        {
            // Start reading.
            while (rdr.Read())
            {
                // Display OnsiteCourse's location.
                Console.WriteLine("CourseID: {0} ", rdr["CourseID"]);
                Console.WriteLine("Location: {0} ", rdr["Location"]);
            }
        }
    }
}
Using conn As New EntityConnection("name=SchoolEntities")
    conn.Open()
    ' Create a query that specifies to 
    ' get a collection of only OnsiteCourses. 

    Dim esqlQuery As String = "SELECT VAlUE onsiteCourse FROM " & _
        "OFTYPE(SchoolEntities.Courses, SchoolModel.OnsiteCourse) AS onsiteCourse"
    Using cmd As New EntityCommand(esqlQuery, conn)
        ' Execute the command. 
        Using rdr As DbDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
            ' Start reading. 
            While rdr.Read()
                ' Display OnsiteCourse's location. 
                Console.WriteLine("CourseID: {0} ", rdr("CourseID"))
                Console.WriteLine("Location: {0} ", rdr("Location"))
            End While
        End Using
    End Using
End Using

请参阅See also