Viewing the generated SQL statements
You might wonder which SQL statements are used by LINQ to Entities to interact with the databases. In this section, we will use two ways to view the generated SQL statements used by LINQ to Entities queries. The first one is to use the ToString
method and the second one is to use SQL Profiler.
Viewing the SQL statements using ToString
First, let's write a new test method to contain one LINQ to Entities query:
static void ViewGeneratedSQL() { using(var NWEntities = new NorthwindEntities()) { var beverages = from p in NWEntities.Products where p.Category.CategoryName == "Beverages" orderby p.ProductName select p; } }
Now, we can print out the SQL statement of the LINQ to Entities query using the following statement:
// view SQL using ToString method Console.WriteLine("The SQL statement is:" +...