如何:查看存储命令(实体框架)
本主题演示如何使用 ToTraceString 方法来查看实体框架中 ObjectQuery 的存储命令。 还可以使用 EntityCommand 类的 ToTraceString 方法。
本主题中的示例演示如何查看一个查询的存储命令,该查询从 Production.Product 表返回 Product 对象。 将使用以下每种实体框架 查询技术演示同一示例:
LINQ to Entities
Entity SQL 以及 ObjectQuery<T>
ObjectQuery<T> 的查询生成器方法
本主题中的示例基于 Adventure Works 销售模型。若要运行本主题中的代码,则必须已经将 Adventure Works 销售模型添加到了您的项目中,并且已经将项目配置为使用实体框架。有关更多信息,请参见如何:使用实体数据模型向导(实体框架)或如何:手动配置实体框架项目和如何:手动定义实体数据模型(实体框架)。
示例
这是 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());
}