如何:查看存储命令(实体框架)
本主题演示如何使用 ToTraceString 方法来查看实体框架中 ObjectQuery 的存储命令。本主题中的示例演示如何查看一个查询的存储命令,该查询从 Production.Product 表返回 Product 对象。将使用以下每种 实体框架 查询技术演示同一示例:
LINQ to Entities
Entity SQL with ObjectQuery<T>
ObjectQuery <T> 的查询生成器方法
本主题中的示例基于 Adventure Works 销售模型。若要运行本示例中的代码,必须已将 AdventureWorks 销售模型添加到您的项目中,并将项目配置为使用实体框架。为此,请完成如何:手动配置实体框架项目和如何:手动定义实体数据模型(实体框架) 中的过程。也可以使用实体数据模型向导定义 AdventureWorks 销售模型。有关更多信息,请参见如何:使用实体数据模型向导(实体框架)。
示例
这是 LINQ to Entities 示例。
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());
}
}
这是 Entity SQL 示例。
Using advWorksContext As New AdventureWorksEntities
Try
' Define the Entity SQL query string.
Dim queryString As String = _
"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());
}
}
这是查询生成器方法示例。
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());
}
}
另请参见
概念
对象查询(实体框架)
查询生成器方法(实体框架)
Entity SQL 概述