Tom Fischer

  Home  |   Contact  |   Syndication    |   Login
  21 Posts | 0 Stories | 47 Comments | 0 Trackbacks

News

Archives

Post Categories

.NET Framwork

TFS

Tools

Visual Studio

Finally I had time to start digging into the Entity Framework.

 I found two dnrTV videos which I highly recommend to get started.
http://www.dnrtv.com/default.aspx?showNum=117
http://www.dnrtv.com/default.aspx?showNum=118

 The idea of this and hopefully follow up blogs is to have my own personal Snippets.

Here comes the first one: How to see the generated SQL

Here are the steps:

 • create your Entity Model (.edmx) from your DB and then
• Get an instance of your Context.
• Write the Query in Linq based on your contex
• Call ToTraceString()

Example:

using (var ctx = new MyDBEntities())
{
    var query = from ev in ctx.Events
                select ev;
    var query2 = query as ObjectQuery<Event>;
    Console.WriteLine(query2.ToTraceString());

    foreach (var ev in ctx.Events)
    {
        Console.WriteLine(ev);
    }
}

Tom

posted on Wednesday, January 28, 2009 8:39 AM

Feedback

# re: Entity Framework: How to see the generated/underlying SQL 11/2/2009 8:12 AM Gianni
Thank you!
This is really useful!

# re: Entity Framework: How to see the generated/underlying SQL 12/6/2009 11:09 PM Ruwan
Thanks a lot. This is what I was searching for a long time

# re: Entity Framework: How to see the generated/underlying SQL 1/25/2010 2:24 AM soujanya
Hi Tom,This is very good. I have a small doubt. This is worked if we read data from asingle table. But using joins i have read data from multible tables then how to read the tracestring(). Because in this situation its not possible to type cast our query to a single table.

# re: Entity Framework: How to see the generated/underlying SQL 1/25/2010 11:22 AM pdurica
var query2 = query1 as ObjectQuery;

works as well

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: