Share via


Procedura: visualizzare i comandi di archiviazione (Entity Framework)

In questo argomento viene illustrato come utilizzare il metodo ToTraceString per visualizzare i comandi di archiviazione per un oggetto ObjectQuery in Entity Framework. Negli esempi in questo argomento viene illustrato come visualizzare i comandi di archiviazione per una query che restituisce oggetti Product dalla tabella Production.Product. Lo stesso esempio viene illustrato utilizzando ognuna delle tecnologie di query Entity Framework seguenti:

  • LINQ to Entities

  • Entity SQL con ObjectQuery<T>

  • Metodi del generatore di query di ObjectQuery<T>

L'esempio incluso in questo argomento è basato sul modello Sales di AdventureWorks. Per eseguire il codice incluso in questo esempio, è necessario avere già aggiunto il modello Sales di AdventureWorks al progetto e avere configurato il progetto per l'utilizzo di Entity Framework. A tale scopo, completare le procedure descritte in Procedura: configurare manualmente un progetto di Entity Framework e Procedura: definire manualmente un modello EDM (Entity Framework). È inoltre possibile utilizzare la procedura guidata Entity Data Model per definire il modello Sales di AdventureWorks. Per ulteriori informazioni, vedere Procedura: utilizzare la procedura guidata Entity Data Model (Entity Framework).

Esempio

Di seguito è riportato un esempio di utilizzo di 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());
    }
}

Di seguito è riportato un esempio di utilizzo di 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());
    }
}

Di seguito è riportato l'esempio del metodo del generatore di query.

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

Vedere anche

Concetti

Query di oggetto (Entity Framework)
Metodi del generatore di query (Entity Framework)
Panoramica su Entity SQL

Altre risorse

LINQ to Entities
Esecuzione di query su Entity Data Model (attività di Entity Framework)