Procedura: visualizzare i comandi di archiviazione (Entity Framework)

In questo argomento viene illustrato come utilizzare il metodo ToTraceString per visualizzare i comandi di archiviazione per un oggetto ObjectQuery in Entity Framework. Il metodo ToTraceString è disponibile anche nella classe EntityCommand.

Negli esempi in questo argomento viene illustrato come visualizzare i comandi di archiviazione per una query che restituisce oggetti Product dalla tabella Production.Product. Lo stesso esempio viene illustrato utilizzando ognuna delle tecnologie di query Entity Framework seguenti:

  • LINQ to Entities

  • Entity SQL con ObjectQuery<T>

  • Metodi del generatore di query di ObjectQuery<T>

L'esempio incluso in questo argomento è basato sul modello Sales di AdventureWorks. Per eseguire il codice incluso in questo argomento, è necessario avere già aggiunto il modello Sales di AdventureWorks al progetto e avere configurato il progetto per l'utilizzo di Entity Framework. Per ulteriori informazioni, vedere Procedura: utilizzare la Procedura guidata Entity Data Model (Entity Framework) o Procedura: configurare manualmente un progetto di Entity Framework e Procedura: definire manualmente un modello EDM (Entity Framework).

Esempio

Di seguito viene illustrato un esempio di LINQ to Entities .

Dim productID = 900
Using context As New AdventureWorksEntities()
    ' Define an ObjectSet to use with the LINQ query. 
    Dim products As ObjectSet(Of Product) = context.Products

    ' Define a LINQ query that returns a selected product. 
    Dim result = From product In products _
        Where product.ProductID = productID _
        Select product

    ' Cast the inferred type var to an ObjectQuery 
    ' and then write the store commands for the query. 
    Console.WriteLine(DirectCast(result, ObjectQuery(Of Product)).ToTraceString())
End Using
int productID = 900;
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Define an ObjectSet to use with the LINQ query.
    ObjectSet<Product> products = context.Products;

    // Define a LINQ query that returns a selected product.
    var result = from product in products
                 where product.ProductID == productID
                 select product;

    // Cast the inferred type var to an ObjectQuery
    // and then write the store commands for the query.
    Console.WriteLine(((ObjectQuery<Product>)result).ToTraceString());
}

Di seguito viene illustrato un esempio di Entity SQL .

Dim productID = 900

Using context As New AdventureWorksEntities()
    ' Define the Entity SQL query string. 
    Dim queryString As String = "SELECT VALUE product FROM AdventureWorksEntities.Products AS product " & _
        " WHERE product.ProductID = @productID"

    ' Define the object query with the query string. 
    Dim productQuery As New ObjectQuery(Of Product)(queryString, context, MergeOption.AppendOnly)
    productQuery.Parameters.Add(New ObjectParameter("productID", productID))

    ' Write the store commands for the query. 
    Console.WriteLine(productQuery.ToTraceString())
End Using
int productID = 900;
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Define the Entity SQL query string.
    string queryString =
        @"SELECT VALUE product FROM AdventureWorksEntities.Products AS product 
      WHERE product.ProductID = @productID";

    // Define the object query with the query string.
    ObjectQuery<Product> productQuery =
        new ObjectQuery<Product>(queryString, context, MergeOption.AppendOnly);

    productQuery.Parameters.Add(new ObjectParameter("productID", productID));

    // Write the store commands for the query.
    Console.WriteLine(productQuery.ToTraceString());
}

Di seguito viene fornito un esempio del metodo del generatore di query.

Dim productID = 900
Using context As New AdventureWorksEntities()
    ' Define the object query for the specific product. 
    Dim productQuery As ObjectQuery(Of Product) = context.Products.Where("it.ProductID = @productID")
    productQuery.Parameters.Add(New ObjectParameter("productID", productID))

    ' Write the store commands for the query. 
    Console.WriteLine(productQuery.ToTraceString())
End Using
int productID = 900;
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Define the object query for the specific product.
    ObjectQuery<Product> productQuery =
        context.Products.Where("it.ProductID = @productID");
    productQuery.Parameters.Add(new ObjectParameter("productID", productID));

    // Write the store commands for the query.
    Console.WriteLine(productQuery.ToTraceString());
}

Vedere anche

Concetti

Query di oggetto (Entity Framework)
Esecuzione di query su un modello concettuale (Entity Framework)
LINQ to Entities
Panoramica su Entity SQL