Cómo ver los comandos de almacenamiento (Entity Framework)

En este tema se muestra cómo utilizar el método ToTraceString para ver los comandos de almacenamiento de una ObjectQuery en Entity Framework. El método ToTraceString también está disponible en la clase EntityCommand.

Los ejemplos de este tema muestran cómo ver los comandos de almacenamiento de una consulta que devuelve objetos Product desde la tabla Production.Product. Se muestra el mismo ejemplo usando cada una de las siguientes tecnologías de consulta de Entity Framework :

  • LINQ to Entities

  • Entity SQL con ObjectQuery<T>

  • Métodos del generador de consultas de ObjectQuery<T>

El ejemplo de este tema se basa en el modelo AdventureWorks Sales. Para ejecutar el código de este tema, debe haber agregado el modelo AdventureWorks Sales al proyecto y haber configurado el proyecto para que use Entity Framework. Para obtener más información, vea Cómo: Usar el Asistente para Entity Data Model (Entity Framework) o Cómo: Configurar manualmente un proyecto de Entity Framework y Cómo: Definir manualmente un modelo Entity Data Model (Entity Framework).

Ejemplo

Este es el ejemplo de 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());
}

Este es el ejemplo de 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());
}

A continuación se muestra el ejemplo del método del generador de consultas.

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

Vea también

Conceptos

Consultas de objeto (Entity Framework)
Consultar un modelo conceptual (Entity Framework)
LINQ to Entities
Información general de Entity SQL