다음을 통해 공유


방법: 저장소 명령 보기(Entity Framework)

이 항목에서는 ToTraceString 메서드를 사용하여 Entity Framework에서 ObjectQuery에 대한 저장소 명령을 표시하는 방법을 보여 줍니다. EntityCommand 클래스에서 ToTraceString 메서드를 사용할 수도 있습니다.

이 항목의 예제에서는 Production.Product 테이블에서 Product 개체를 반환하는 쿼리에 대한 저장소 명령을 표시하는 방법을 보여 줍니다. 다음의 Entity Framework 쿼리 기술을 각각 사용하여 동일한 예제를 보여 줍니다.

  • LINQ to Entities

  • ObjectQuery<T>가 포함된 Entity SQL

  • ObjectQuery<T>의 쿼리 작성기 메서드

이 항목의 예제는 Adventure Works Sales 모델을 기반으로 합니다. 이 항목의 코드를 실행하려면 프로젝트에 Adventure Works Sales 모델을 추가하고 프로젝트에서 Entity Framework를 사용하도록 구성해야 합니다. 자세한 내용은 방법: 엔터티 데이터 모델 마법사 사용(Entity Framework) 또는 방법: Entity Framework 프로젝트 수동 구성방법: 엔터티 데이터 모델 수동 정의(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 개요