共用方式為


HOW TO:檢視存放區命令 (Entity Framework)

本主題示範如何使用 ToTraceString 方法來檢視 Entity Framework 中 ObjectQuery 的存放區命令。本主題的範例會示範如何針對從 Production.Product 資料表傳回 Product 物件的查詢來檢視存放命令。此外,也會顯示使用下列每一個 實體架構 查詢技術的相同範例:

  • LINQ to Entities

  • 使用 ObjectQuery<T> 的 Entity SQL

  • ObjectQuery<T> 的查詢產生器方法

本主題的範例是根據 Adventure Works Sales Model。若要執行此範例中的程式碼,您必須已經將 AdventureWorks Sales Model 加入到專案中,並設定您的專案使用 Entity Framework。若要這樣做,請完成 HOW TO:手動設定 Entity Framework 專案HOW TO:以手動方式定義 Entity Data Model (Entity Framework) 中的程序。您也可以使用 [Entity Data Model 精靈] 定義 AdventureWorks Sales Model。如需詳細資訊,請參閱 HOW TO:使用 Entity Data Model 精靈 (Entity Framework)

範例

這是 LINQ 到實體 範例。

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());
    }
}

這是 實體 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 Framework)
查詢產生器方法 (Entity Framework)
Entity SQL 概觀

其他資源

LINQ to Entities
查詢 Entity Data Model (Entity Framework 工作)