Freigeben über


Gewusst wie: Navigieren in Beziehungen mit dem 'Navigate'-Operator (EntityClient)

In diesem Thema wird anhand eines Beispiels gezeigt, wie ein Befehl für ein Entity Data Model unter Verwendung von EntityCommand ausgeführt wird, und wie die RefType-Ergebnisse mithilfe von EntityDataReader abgerufen werden.

So führen Sie den Code in diesem Beispiel aus

  1. Fügen Sie das AdventureWorks Sales-Modell dem Projekt hinzu, und konfigurieren Sie das Projekt für die Verwendung von Entity Framework. Gehen Sie dazu folgendermaßen vor:

  2. Fügen Sie der Codepage Ihrer Anwendung die folgenden using-Anweisungen (Imports in Visual Basic) hinzu:

    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;
    

Beispiel

Das folgende Beispiel zeigt, wie in Entity SQL Beziehungen mithilfe des NAVIGATE (Entity SQL)-Operators navigiert werden können. Der Navigate-Operator benötigt die folgenden Parameter: eine Instanz einer Entität, den Beziehungstyp, das Ende der Beziehung und den Anfang der Beziehung. Die vereinfachte Version des Navigate-Operators benötigt eine Instanz einer Entität und den Beziehungstyp. Das folgende Beispiel zeigt die Verwendung des vereinfachten Navigate-Operators.

Using connection As EntityConnection = New EntityConnection("name=AdventureWorksEntities")
    connection.Open()

    Try
        ' Create an EntityCommand and pass the connection object 
        ' and Entity SQL query to its constructor.
        Using cmd As EntityCommand = connection.CreateCommand()
            ' Create an Entity SQL query.
            Dim esqlQuery As String = "SELECT address.AddressID, (SELECT VALUE DEREF(soh) FROM " & _
                "NAVIGATE(address, AdventureWorksModel.FK_SalesOrderHeader_Address_BillToAddressID) " & _
                "AS soh) FROM AdventureWorksEntities.Address AS address"

            cmd.CommandText = esqlQuery
            ' Execute the command.
            Using reader As DbDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
                ' Start reading.
                Do While reader.Read
                    Console.WriteLine(reader.Item("AddressID"))
                Loop
            End Using
        End Using
    Catch ex As EntityException
        Console.WriteLine(ex.ToString())
    End Try
    connection.Close()
End Using
using (EntityConnection conn =
    new EntityConnection("name=AdventureWorksEntities"))
{
    conn.Open();
    try
    {
        // Create an EntityCommand.
        using (EntityCommand cmd = conn.CreateCommand())
        {
            // Create an Entity SQL query.
            string esqlQuery =
                @"SELECT address.AddressID, (SELECT VALUE DEREF(soh) FROM 
              NAVIGATE(address, AdventureWorksModel.FK_SalesOrderHeader_Address_BillToAddressID) 
              AS soh) FROM AdventureWorksEntities.Address AS address";

            cmd.CommandText = esqlQuery;

            // Execute the command.
            using (DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
            {
                // Start reading.
                while (rdr.Read())
                {
                    Console.WriteLine(rdr["AddressID"]);
                }
            }
        }
    }
    catch (EntityException ex)
    {
        Console.WriteLine(ex.ToString());
    }
    conn.Close();
}

Siehe auch

Konzepte

Entity SQL-Sprache

Weitere Ressourcen

Arbeiten mit EntityClient (Entity Framework-Aufgaben)
How to: Execute an Entity SQL Query Using EntityCommand