How to: View the Store Commands (Entity Framework)

This topic shows how to use the ToTraceString method to view the store commands for an ObjectQuery in the Entity Framework. The examples in this topic show how to view the store commands for a query that returns Product objects from the Production.Product table. The same example is shown using each of the following Entity Framework query technologies:

  • LINQ to Entities

  • Entity SQL with ObjectQuery<T>

  • Query builder methods of ObjectQuery<T>

The example in this topic is based on the Adventure Works Sales Model. To run the code in this example, you must have already added the AdventureWorks Sales Model to your project and configured your project to use the Entity Framework. To do this, complete the procedures in How to: Manually Configure an Entity Framework Project and How to: Manually Define an Entity Data Model (Entity Framework). You can also use the Entity Data Model Wizard to define the AdventureWorks Sales Model. For more information, see How to: Use the Entity Data Model Wizard (Entity Framework).

Example

This is the LINQ to Entities example.

Using advWorksContext As New AdventureWorksEntities()
    Try
        ' Define an ObjectQuery to use with the LINQ query.
        Dim products As ObjectQuery(Of Product) = advWorksContext.Product

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

        ' Cast the inferred type to a ObjectQuery
        ' and then write the store commands for the query.
        Console.WriteLine(CType(result, ObjectQuery(Of Product)).ToTraceString())
    Catch ex As EntitySqlException
        Console.WriteLine(ex.ToString())
    End Try
End Using
using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
{
    try
    {
        // Define an ObjectQuery to use with the LINQ query.
        ObjectQuery<Product> products = advWorksContext.Product;

        // Define a LINQ query that returns a selected product.
        var result = from product in products 
                     where product.ProductID == 900
                     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());
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

This is the Entity SQL example.

Using advWorksContext As New AdventureWorksEntities
    Try
        ' Define the Entity SQL query string.
        Dim queryString = _
            "SELECT VALUE Product FROM AdventureWorksEntities.Product AS Product " & _
            "WHERE Product.ProductID = 900"

        ' Define the object query with the query string.
        Dim productQuery As New ObjectQuery(Of Product) _
            (queryString, advWorksContext, MergeOption.AppendOnly)

        ' Write the store commands for the query.
        Console.WriteLine(productQuery.ToTraceString())
    Catch ex As EntitySqlException
        Console.WriteLine(ex.ToString)
    End Try
End Using
using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
{
    try
    {
        // Define the Entity SQL query string.
        string queryString =
            @"SELECT VALUE Product FROM AdventureWorksEntities.Product AS Product 
          WHERE Product.ProductID = 900";

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

        // Write the store commands for the query.
        Console.WriteLine(productQuery.ToTraceString());
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

This is the query builder method example.

Using advWorksContext As New AdventureWorksEntities()
    Try
        ' Define the object query for the specific product.
        Dim productQuery As ObjectQuery(Of Product) = _
            advWorksContext.Product.Where("it.ProductID = 900")

        ' Write the store commands for the query.
        Console.WriteLine(productQuery.ToTraceString())
    Catch ex As EntitySqlException
        Console.WriteLine(ex.ToString())
    End Try
End Using
using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
{
    try
    {
        // Define the object query for the specific product.
        ObjectQuery<Product> productQuery =
            advWorksContext.Product.Where("it.ProductID = 900");

        // Write the store commands for the query.
        Console.WriteLine(productQuery.ToTraceString());
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

See Also

Concepts

Object Queries (Entity Framework)
Query Builder Methods (Entity Framework)
Entity SQL Overview

Other Resources

LINQ to Entities
Querying an Entity Data Model (Entity Framework Tasks)