次の方法で共有


Navigate 演算子でリレーションシップをナビゲートする方法 (EntityClient)

このトピックでは、EntityCommand を使用して Entity Data Model に対してコマンドを実行する方法、および EntityDataReader を使用して RefType 結果を取得する方法の例を示します。

この例のコードを実行するには

  1. AdventureWorks Sales Model をプロジェクトに追加し、Entity Framework を使用するようにプロジェクトを構成します。これを行うには、次のいずれかの操作を実行します。

  2. アプリケーションのコード ページで、次の 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;
    

次の例では、Entity SQL で NAVIGATE (Entity SQL) 演算子を使用してリレーションシップをナビゲートする方法を示しています。Navigate 演算子は、エンティティのインスタンス、リレーションシップの種類、リレーションシップの終端エンティティ、および、リレーションシップの開始エンティティをパラメータとして受け取ります。Navigate 演算子には、エンティティのインスタンスとリレーションシップの種類だけを受け取る、より単純な形態もあります。次の例は、より単純な形態の Navigate 演算子の使用法を示しています。

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();
}

参照

概念

Entity SQL 言語

その他のリソース

EntityClient の使用 (Entity Framework タスク)
How to: Execute an Entity SQL Query Using EntityCommand