Как просмотреть команды хранилища (платформа Entity Framework)
В этом разделе показано, с помощью метода ToTraceString просмотреть команды хранилища для ObjectQuery в платформе Entity Framework. Метод ToTraceString также доступен в классе EntityCommand.
В примерах этого раздела объясняется, как просмотреть команды хранения для запроса, возвращающего объекты Product из таблицы Production.Product. Один и тот же пример приводится с использованием всех следующих технологий запросов платформы Entity Framework .
LINQ to Entities
Entity SQL с ObjectQuery<T>
Методы построителя запросов ObjectQuery<T>
Пример в этом разделе основан на модели Adventure Works Sales. Чтобы запустить код, используемый в данном разделе, нужно сначала добавить к проекту модель Adventure Works Sales и настроить его для использования платформы Entity Framework. Дополнительные сведения см. в разделе Как использовать мастер моделей EDM (Entity Framework) или Как вручную настроить проект Entity Framework и Как определить модель EDM вручную (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