How to: Navigate Relationships with Navigate Operator (EntityClient)

This topic provides an example of how to execute a command against an Entity Data Model by using EntityCommand, and how to retrieve the RefType results by using EntityDataReader.

To run the code in this example

  1. Add the AdventureWorks Sales Model to your project and configure your project to use the Entity Framework. To do this, do one of the following:

  2. In the code page for your application, add the following using statements (Imports in Visual Basic):.

    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


The following example shows how to navigate relationships in Entity SQL by using the NAVIGATE (Entity SQL) operator. The Navigate operator takes the following parameters: an instance of an entity, the relationship-type, the end of the relationship, and the beginning of the relationship. The simplier version of the Navigate operator takes an instance of an entity and the relationship-type. The following example shows how to use the simplier version of the Navigate operator.

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

        ' 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
            End Using
        End Using
    Catch ex As EntityException
    End Try
End Using

See Also

Community Additions