简体   繁体   中英

Viewing Entity SQL produced by Linq-to-Entities

Is there a way I can view the Entity Sql (eSQL) that my Linq-to-entities queries are generating with EF framework (that is, not native SQL, but eSQL, if that makes sense?)

Thanks!

You can't. It is not generated.
Actually, LINQ to Entities queries are translated directly into Expression Tree, and the nodes of this Expression Tree are translated into SQL clauses, and then integrated into a SQL query. No Entity SQL.

var query1 = from person in Database
           select person.Name;

You can cast the query1 into ObjectQuery and use the ToTraceString method to see the query.

Console.WriteLine(((ObjectQuery)query1).ToTraceString());

To view linq query in development environment.

  1. You assign your query in IQueryable variable.
  2. Plase debug point below the query in next line. So, Query get executed.
  3. Place mouse over the IQueryable variable. You will be able to see the SQL query.
  4. You can copy your sql query in SQL query analyser and execute it.

view example in image http://i.stack.imgur.com/t6PK6.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM