Tom Fischer

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

News

Archives

Post Categories

.NET Framwork

TFS

Tools

Visual Studio

Wednesday, January 28, 2009 #

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