HOW TO:檢視存放區命令 (Entity Framework)
本主題示範如何使用 ToTraceString 方法來檢視 Entity Framework 中 ObjectQuery 的存放區命令。 ToTraceString 方法也可以在 EntityCommand 類別上取得。
本主題的範例會示範如何針對從 Production.Product 資料表傳回 Product 物件的查詢來檢視存放命令。 相同範例會使用下列每個 Entity Framework 查詢技術來顯示:
LINQ to Entities
Entity SQL with ObjectQuery<T>
Query builder methods of ObjectQuery<T>
本主題的範例根據 Adventure Works Sales Model。若要執行此主題中的程式碼,您必須已經將 Adventure Works Sales Model 加入到專案中,並設定您的專案使用 Entity Framework。如需詳細資訊,請參閱 HOW TO:使用實體資料模型精靈 (Entity Framework) 或 HOW TO:手動設定 Entity Framework 專案及 HOW TO:手動設定 Entity Framework 專案。
範例
這是 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());
}
這是 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());
}
這是查詢產生器方法範例。
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());
}
另請參閱
概念
物件查詢 (Entity Framework)
查詢概念模型 (Entity Framework)
LINQ to Entities
Entity SQL 概觀