Share via


Postupy: Procházení relací pomocí navigačního operátoru

Toto téma ukazuje, jak spustit příkaz proti konceptuálnímu modelu pomocí EntityCommand objektu a jak načíst RefType výsledky pomocí objektu EntityDataReader.

Spuštění kódu v tomto příkladu

  1. Přidejte do projektu model AdventureWorks Sales Model a nakonfigurujte projekt tak, aby používal Entity Framework. Další informace naleznete v tématu Postupy: Použití Průvodce datovým modelem entity.

  2. Na znakovou stránku aplikace přidejte následující using příkazy (Imports v jazyce Visual Basic):

    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 System.Data.Metadata.Edm;
    
    Imports System.Collections.Generic
    Imports System.Collections
    Imports System.Data.Common
    Imports System.Data
    Imports System.IO
    Imports System.Data.SqlClient
    Imports System.Data.EntityClient
    Imports System.Data.Metadata.Edm
    
    

Příklad

Následující příklad ukazuje, jak procházet relace v Entity SQL pomocí operátoru NAVIGATE . Operátor Navigate přebírá následující parametry: instanci entity, typ relace, konec relace a začátek relace. Volitelně můžete operátoru předat pouze instanci entity a typ Navigate relace.

using (EntityConnection conn =
    new EntityConnection("name=AdventureWorksEntities"))
{
    conn.Open();
    // 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.Addresses AS address";

        cmd.CommandText = esqlQuery;

        // Execute the command.
        using (DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
        {
            // Start reading.
            while (rdr.Read())
            {
                Console.WriteLine(rdr["AddressID"]);
            }
        }
    }
    conn.Close();
}
Using conn As New EntityConnection("name=AdventureWorksEntities")
    conn.Open()
    ' Create an EntityCommand. 
    Using cmd As EntityCommand = conn.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.Addresses AS address"


        cmd.CommandText = esqlQuery

        ' Execute the command. 
        Using rdr As DbDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
            ' Start reading. 
            While rdr.Read()
                Console.WriteLine(rdr("AddressID"))
            End While
        End Using
    End Using
    conn.Close()
End Using

Viz také