Condividi tramite


Procedura: eseguire una query che restituisce tipi complessi (EntityClient)

In questo argomento viene illustrato come eseguire una query Entity SQL che restituisce tipi complessi utilizzando EntityCommand. Nell'esempio vengono utilizzati gli schemi definiti in Procedura: definire un modello con un tipo complesso (Entity Framework). Per informazioni sulla configurazione del progetto e un esempio di come eseguire una query che restituisce tipi complessi utilizzando Object Services, vedere Procedura: creare ed eseguire query di oggetto con tipi complessi (Entity Framework).

Esempio

Nell'esempio seguente viene illustrato come creare ed eseguire una query con un tipo complesso. Il tipo complesso rappresenta un tipo che include un insieme di proprietà, come un tipo di entità, ma non una proprietà chiave. La proprietà Address dell'entità CCustomer viene implementata come tipo complesso. Nell'esempio seguente vengono restituite due proprietà di tipo CCustomer: CustomerId e Address. Poiché Address è un tipo complesso, il codice restituisce valori delle proprietà di 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();
}

Vedere anche

Altre risorse

Utilizzo di EntityClient (attività di Entity Framework)