本主题提供一个示例,演示如何通过使用 EntityCommand 对实体数据模型执行命令,以及如何使用 EntityDataReader 检索嵌套集合结果。
运行本示例中的代码
- 将 AdventureWorks 销售模型添加到项目并将项目配置为使用实体框架。为此,请执行下列操作之一: - 如果已安装实体数据模型工具,请完成如何:使用实体数据模型向导(实体框架) 中的过程。 
- 否则,请完成如何:手动配置实体框架项目和如何:手动定义实体数据模型(实体框架) 中的过程。 
 
- 在应用程序的代码页中,添加以下 using 语句(在 Visual Basic 中为 Imports): - Imports System Imports System.Collections.Generic Imports System.Collections Imports System.Data.Common Imports System.Data Imports System.Data.SqlClient Imports System.Data.EntityClient Imports System.Data.Metadata.Edm Imports System.IO ' Add AdventureWorksModel prepended with the root namespace for the project. 'Imports ProjectName.AdventureWorksModel- 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 AdventureWorksModel; using System.Data.Metadata.Edm;
示例
嵌套集合是指位于另一个集合内的集合。以下代码检索 Contacts 集合以及与每个 Contact 关联的 SalesOrderHeaders 的嵌套集合。
Using conn As EntityConnection = New EntityConnection("name=AdventureWorksEntities")
    conn.Open()
    Try
        ' Create an EntityCommand.
        Using cmd As EntityCommand = conn.CreateCommand()
            ' Create a nested query.
            Dim esqlQuery As String = "Select c.ContactID, c.SalesOrderHeader " & _
                    "From AdventureWorksEntities.Contact as c"
            cmd.CommandText = esqlQuery
            ' Execute the command.
            Using rdr As EntityDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
                ' The result returned by this query contains 
                ' ContactID and a nested collection of SalesOrderHeader items.
                ' associated with this Contact.
                Do While rdr.Read
                    ' the first column contains Contact ID.
                    Console.WriteLine("Contact ID: {0}", rdr.Item("ContactID"))
                    ' The second column contains a collection of SalesOrderHeader 
                    ' items associated with the Contact.
                    Dim nestedReader As DbDataReader = rdr.GetDataReader(1)
                    Do While nestedReader.Read
                        Console.WriteLine("   SalesOrderID: {0} ", nestedReader.Item("SalesOrderID"))
                        Console.WriteLine("   OrderDate: {0} ", nestedReader.Item("OrderDate"))
                    Loop
                Loop
            End Using
        End Using
    Catch ex As EntityException
        Console.WriteLine(ex.ToString())
    End Try
    conn.Close()
End Using
using (EntityConnection conn =
    new EntityConnection("name=AdventureWorksEntities"))
{
    conn.Open();
    try
    {
        // Create an EntityCommand.
        using (EntityCommand cmd = conn.CreateCommand())
        {
            // Create a nested query.
            string esqlQuery =
                @"Select c.ContactID, c.SalesOrderHeader
            From AdventureWorksEntities.Contact as c";
            cmd.CommandText = esqlQuery;
            // Execute the command.
            using (EntityDataReader rdr =
                cmd.ExecuteReader(CommandBehavior.SequentialAccess))
            {
                // The result returned by this query contains 
                // ContactID and a nested collection of SalesOrderHeader items.
                // associated with this Contact.
                while (rdr.Read())
                {
                    // the first column contains Contact ID.
                    Console.WriteLine("Contact ID: {0}", rdr["ContactID"]);
                    // The second column contains a collection of SalesOrderHeader 
                    // items associated with the Contact.
                    DbDataReader nestedReader = rdr.GetDataReader(1);
                    while (nestedReader.Read())
                    {
                        Console.WriteLine("   SalesOrderID: {0} ", nestedReader["SalesOrderID"]);
                        Console.WriteLine("   OrderDate: {0} ", nestedReader["OrderDate"]);
                    }
                }
            }
        }
    }
    catch (EntityException ex)
    {
        Console.WriteLine(ex.ToString());
    }
    conn.Close();
}