How to: View the Store Commands
This topic shows how to use the ToTraceString method to view the store commands for an ObjectQuery in the Entity Framework. The ToTraceString method is also avilable on the EntityCommand class.
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 topic, you must have already added the Adventure Works Sales Model to your project and configured your project to use the Entity Framework. For more information, see How to: Use the Entity Data Model Wizard (Entity Framework) or How to: Manually Configure an Entity Framework Project and How to: Manually Define an Entity Data Model (Entity Framework).
Example
This is the LINQ to Entities example.
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());
}
This is the Entity SQL example.
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());
}
This is the query builder method example.
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());
}
See Also
Concepts
Object Queries
Querying a Conceptual Model
LINQ to Entities
Entity SQL Overview