Share via


格納コマンドを表示する方法 (Entity Framework)

このトピックでは、ToTraceString メソッドを使用して、Entity Framework の ObjectQuery に対する格納コマンドを表示する方法を示します。 ToTraceString メソッドは、EntityCommand クラスでも使用できます。

このトピックの例では、Production.Product テーブルから Product オブジェクトを返すクエリに対する格納コマンドを表示する方法を示します。 The same example is shown using each of the following Entity Framework query technologies:

  • LINQ to Entities

  • Entity SQL と ObjectQuery<T>

  • ObjectQuery<T> のクエリ ビルダー メソッド

このトピックの例には、Adventure Works Sales Model が使用されています。このトピックのコードを実行するには、あらかじめプロジェクトに Adventure Works Sales Model を追加し、Entity Framework を使用するようにプロジェクトを構成しておく必要があります。詳細については、「Entity Data Model ウィザードを使用する方法 (Entity Framework)」、または「Entity Framework プロジェクトを手動で構成する方法」、および「Entity Data Model を手動で定義する方法 (Entity Framework)」を参照してください。

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

これは、クエリ ビルダー メソッドの例です。

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 の概要