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

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

  • LINQ to Entities

  • ObjectQuery<T>를 사용한 Entity SQL

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

이 항목의 예제는 Adventure Works Sales 모델을 기반으로 합니다. 이 예제의 코드를 실행하려면 프로젝트에 AdventureWorks Sales 모델을 추가하고 Entity Framework를 사용하도록 프로젝트를 구성해야 합니다. 이렇게 하려면 방법: Entity Framework 프로젝트 수동 구성방법: 엔터티 데이터 모델 수동 정의(Entity Framework)의 절차를 수행합니다. 엔터티 데이터 모델 마법사를 사용하여 AdventureWorks Sales 모델을 정의할 수도 있습니다. 자세한 내용은 방법: 엔터티 데이터 모델 마법사 사용(Entity Framework)을 참조하십시오.

예제

다음은 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 Framework)
쿼리 작성기 메서드(Entity Framework)
Entity SQL 개요

기타 리소스

LINQ to Entities
엔터티 데이터 모델 쿼리(Entity Framework 작업)