Procédure : afficher les commandes de stockage (Entity Framework)

Cette rubrique montre comment utiliser la méthode ToTraceString pour afficher les commandes de stockage d'un objet ObjectQuery dans Entity Framework. Les exemples de cette rubrique montrent comment afficher les commandes de stockage d'une requête qui retourne des objets Product à partir de la table Production.Product. Le même exemple est repris en utilisant chacune des technologies de requête Entity Framework suivantes :

  • LINQ to Entities

  • Entity SQL avec ObjectQuery<T>

  • Méthodes du Générateur de requêtes d'ObjectQuery<T>

L'exemple de cette rubrique est basé sur le modèle de vente Adventure Works. Pour exécuter le code de cet exemple, vous devez déjà avoir ajouté le modèle de vente AdventureWorks à votre projet et configuré ce dernier pour qu'il utilise Entity Framework. Pour ce faire, exécutez les procédures décrites dans Procédure : configurer manuellement un projet Entity Framework et Procédure : définir manuellement un modèle EDM (Entity Data Model) (Entity Framework). Vous pouvez également définir le modèle de vente AdventureWorks Sales Model à l'aide de l'Assistant EDM. Pour plus d'informations, voir Procédure : utiliser l'Assistant Entity Data Model (Entity Framework).

Exemple

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

Cet exemple utilise Entité 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());
    }
}

Cet exemple utilise la méthode du Générateur de requêtes.

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

Voir aussi

Concepts

Requêtes d'objet (Entity Framework)
Méthodes du Générateur de requêtes (Entity Framework)
Vue d'ensemble d'Entity SQL

Autres ressources

LINQ to Entities
Interrogation d'un modèle EDM (Tâches Entity Framework)