Tracing Queries in Entity Framework 4

As you aware of Entity Framework 4 feature translates LINQ queries to SQL to execute in the backend.  However, what Entity Framework may render a SQL query that looks much different.  Entity Framework’s SQL query building process may involve the use of sub queries, nested joins, or any other construct.  

This query, a LINQ query against the AdventureWorks database, queries a collection of Address objects.  Even though the signature of the query is IQueryable<Address>, the underlying constructs actually are an ObjectQuery<Address> instance.  The ObjectQuery is useful for many things; one of those is the ToTraceString method.  This method returns the underlying SQL query that was executed against the database.  It is very easy to call, as shown below:

The underlying SQL query appears below.  Notice how simple the LINQ query structure is setup above.  In the query above, the LINQ query makes it very easy to drill through two primary key references, from Address to the StateProvince and StateProvinceCode tables, as well as a CustomerAddresses subquery.  The query renders as the following:

If you want to know how exactly your Linq query generate the SQL query then ToTraceString very handy to verifying and underlying LINQ and SQL. Even you can use the database profiling tool also for verifying your query.

Similar Topics:

Tags:

entity framework traceentity framework trace sqlentity framework trace query
We will send you more information on Tracing Queries in Entity Framework 4.
Enter your email address:
This entry was posted in .Net, SQL and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *