locked
Where is the feature DataContext.Log in EntityFramework? RRS feed

  • Question

  • Hi

    In Linq-to-sql there was a gr8 feature to trace log all generate sql statements via

    DataContext.Log  eg.     DataContext.Log = new DebugTextWriter();

    Where is this feature in EntityFramework or what is alternative to above tracing.

    Please Advice.

    Regards

    Monday, April 22, 2013 3:00 PM

Answers

  • Hi JiyaDesai;

    Entity Framework does not have a Log function as in Linq to SQL. If you want to see what the SQL that will be sent to the SQL Server you can do the following.

    // A Entity Framework query that returns a IQueryable
    // The query should NOT end in a function that will execute the query such as Count() or ToList() and so on.
    var query = MyContext.MyTable...;
    // Get the SQL that will be sent to the server once the query is executed
    String sqlCmd = query.ToString()

    It will also work in query format as follow.

    var query = from c in MyContext.MyTable
                where c.Fieldname == someValue
                select c;
    
    String sqlCmd = query.ToString();

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Chester Hong Tuesday, May 7, 2013 5:47 AM
    Tuesday, April 23, 2013 3:34 AM