如何:执行返回复杂类型的查询 (EntityClient)

本主题说明如何使用 EntityCommand 执行返回复杂类型的 Entity SQL 查询。本示例使用在如何:使用复杂类型定义模型(实体框架) 中定义的架构。有关配置项目的信息以及如何使用对象服务执行返回复杂类型的查询的示例,请参见如何:使用复杂类型创建和执行对象查询(实体框架)

示例

以下示例说明如何创建和执行具有复杂类型的查询。复杂类型表示的类型包括一组属性(类似于实体类型),但不包括键属性。CCustomer 实体的 Address 属性实现为复杂类型。以下示例输出 CCustomer 类型的两个属性:CustomerIdAddress。由于 Address 是复杂类型,因此,此代码将输出 Address 的各个属性的值。

Using conn As EntityConnection = New EntityConnection("name=CustomerComplexAddrContext")
    conn.Open()

    ' Create an EntityCommand.
    Using cmd As EntityCommand = conn.CreateCommand()

        ' Create a query that returns Address complex type.
        Dim esqlQuery As String = "SELECT VALUE customers FROM " & _
            "CustomerComplexAddrContext.CCustomers " & _
            "AS customers WHERE customers.CustomerId < 3"
        cmd.CommandText = esqlQuery
        ' Execute the command.
        Using rdr As EntityDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
            ' The result returned by this query contains 
            ' Address complex Types.
            Do While rdr.Read
                ' Display CustomerID
                Console.WriteLine("Customer ID: {0}", _
                    rdr.Item("CustomerId"))
                ' Display Address information.
                Dim nestedRecord As DbDataRecord = DirectCast(rdr.Item("Address"), DbDataRecord)
                Console.WriteLine("Address:")
                For i = 0 To nestedRecord.FieldCount - 1
                    Console.WriteLine("  " + nestedRecord.GetName(i) & _
                            ": " + nestedRecord.GetValue(i))
                Next i
            Loop
        End Using
    End Using
    conn.Close()
End Using
using (EntityConnection conn =
    new EntityConnection("name=CustomerComplexAddrContext"))
{
    conn.Open();

    // Create a query that returns Address complex type.
    string esqlQuery =
        @"SELECT VALUE customers FROM
            CustomerComplexAddrContext.CCustomers
            AS customers WHERE customers.CustomerId < 3";
    try
    {
        // Create an EntityCommand.
        using (EntityCommand cmd = conn.CreateCommand())
        {
            cmd.CommandText = esqlQuery;
            // Execute the command.
            using (EntityDataReader rdr =
                cmd.ExecuteReader(CommandBehavior.SequentialAccess))
            {
                // The result returned by this query contains 
                // Address complex Types.
                while (rdr.Read())
                {
                    // Display CustomerID
                    Console.WriteLine("Customer ID: {0}",
                        rdr["CustomerId"]);
                    // Display Address information.
                    DbDataRecord nestedRecord =
                        rdr["Address"] as DbDataRecord;
                    Console.WriteLine("Address:");
                    for (int i = 0; i < nestedRecord.FieldCount; i++)
                    {
                        Console.WriteLine("  " + nestedRecord.GetName(i) +
                            ": " + nestedRecord.GetValue(i));
                    }
                }
            }
        }
    }
    catch (EntityException ex)
    {
        Console.WriteLine(ex.ToString());
    }
    conn.Close();
}

另请参见

其他资源

使用 EntityClient(实体框架任务)