How to view the Sql statement generated by Entity Framework?



    Hi ,

    As topic. How can I do it?

    I know how to do it in LINQ to SQL , but not for Entity Framework.

    Thursday, November 22, 2007 5:40 AM


All replies

  • And how do you do it with LINQ to SQL? Smile


    Thursday, November 22, 2007 8:56 PM
  • Imports LinqTest.LinqSql

    Public Class Formdemo

    Private db As New LinqSql.ProductDataContext()

    Private Sub Formdemo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    db.Log = Console.Out  'Add This Line to Your application , and you can view the Sql Statement in the OutPut Windows when Debug the Program


    'Dim emplist = From emp In db.Employees _

    ' Where emp.Id < 20 _

    ' Select emp

    'Me.EmployeeBindingSource.DataSource = emplist



    Dim emplist = db.ExecuteQuery(Of Employee)("select address,city,id,lastname,firstname,postalcode,title from employee where id<50")

    'Dim emplist = db.ExecuteQuery(Of Employee)("select * from employee where id<20")

    Me.EmployeeBindingSource.DataSource = emplist


    'Me.EmployeeBindingSource.DataSource = db.Employees


    End Sub


    Thursday, November 22, 2007 10:48 PM
  • Hello Gordon,

    theres a tool for SQL Server thats called "Profiler". You can view every SQL Statement that is reveived by the Sql server.
    Of Course - you have to work a little with the Debugger to find that out.
    It's maybe not the most beautiful way, buts it works until you can find a way that suits better your needs.
    Thursday, November 22, 2007 10:53 PM
  • Thansk very much Esrever,

    I have used the Sql Profiler before, but I dont want to used it in this Case,

    as you said, it will be the last solution if there is really no other ways to do it.


    Friday, November 23, 2007 1:00 AM

    I am currently use the Sql 2005 Express, It has not Sql Profile.
    Friday, November 23, 2007 1:17 AM
  • try install SQLDeveloper edition. It has enterprise edition level features (including profiler) but it is used for developemtn only Smile

    Aurelian POPA [MVP]


    Friday, November 23, 2007 11:20 PM
  • I beleve the request is how to look at the query in debug mode when querying against EDM in the same way you can with LINQ to SQL, as opposed to using SQL Profiler.


    The trick for it is to get tot eh ENtity Command tree and in there, is the TSQL.


    HEre's a previous thread that shows how to accomplish this.



    Saturday, November 24, 2007 7:45 PM
  • In addition there is a new feature that will arrive with beta 3 of the entity framework (getting closer but still not quite ready yet) that will make this much easier...


    - Danny


    Sunday, November 25, 2007 3:19 AM
  • Thanks Julie Lerman

    you're right. that is waht I need.

    But it is a little bit complex.


    Tuesday, December 04, 2007 1:12 AM
  • >> In addition there is a new feature that will arrive with beta 3 of the entity framework (getting closer but still not quite ready yet) that will make this much easier...


    Any update on what this "feature" is and how to use it now that Beta 3 is out?

    Friday, December 07, 2007 9:06 PM
  • I believe this is implemented through a method named ToTraceString(). If I figure out how to use it before you do, I'll let you know. ;-) and vice-versa.





    Friday, December 07, 2007 9:22 PM
  • Never mind...its doc'd (with example) in the API reference.





    Friday, December 07, 2007 9:26 PM
  • I have visual studio 2008 sp1 installed.

    Where's this new feature?

    Friday, May 23, 2008 2:38 PM
  • There is now a ToTraceString() method on both ObjectQuery and EntityCommand, which returns the store-provider's query statement.


    Friday, May 23, 2008 3:54 PM
  • how you do it in linq to sql man ?? I need to know please !
    Friday, February 17, 2012 5:20 PM