Procedura: spostarsi nelle relazioni con l'operatore Navigate (EntityClient)
In questo argomento viene fornito un esempio per illustrare l'esecuzione di un comando su un modello EDM (Entity Data Model) mediante EntityCommand nonché il recupero dei risultati RefType mediante EntityDataReader.
Per eseguire il codice in questo esempio
Aggiungere il modello Sales di Adventure Works al progetto e configurare il progetto per l'utilizzo di Entity Framework. A tale scopo, eseguire una delle operazioni seguenti:
Se gli strumenti EDM (Entity Dati Model) sono stati installati, completare la procedura descritta in Procedura: utilizzare la procedura guidata Entity Data Model (Entity Framework).
In caso contrario, completare le procedure descritte in Procedura: configurare manualmente un progetto di Entity Framework e in Procedura: definire manualmente un modello EDM (Entity Framework).
Nella tabella codici dell'applicazione aggiungere le istruzioni using seguenti (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
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;
Esempio
Nell'esempio seguente viene illustrato come spostarsi nelle relazioni in Entity SQL utilizzando l'operatore NAVIGATE (Entity SQL). L'operatore Navigate accetta i parametri seguenti: un'istanza di un'entità, il tipo di relazione, l'entità finale della relazione e l'inizio della relazione. La versione più semplice dell'operatore Navigate accetta un'istanza di un'entità e il tipo di relazione. Nell'esempio seguente viene illustrato come utilizzare la versione più semplice dell'operatore 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();
}
Vedere anche
Concetti
Altre risorse
Utilizzo di EntityClient (attività di Entity Framework)
How to: Execute an Entity SQL Query Using EntityCommand