Results 1 to 2 of 2

Thread: How to trace sql generated by Entity Framework query using singleordefault in vs2010

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    35

    How to trace sql generated by Entity Framework query using singleordefault in vs2010

    Hi,

    I'm using Visual Studio 2010 pro, and i'd like to be able to trace the sql generated by the entity framework.
    Code:
     Using ce As New MyDataEntities
                    Dim f = (From c In ce.MYTABLEs
                            Where c.IYEAR = iy And c.ISEQUENCE = isq
                            Select c.IS_PRINTED).SingleOrDefault
    
                    Dim sqlString As String = " "
    
    
                    sqlString = TryCast(f, System.Data.Objects.ObjectQuery).ToTraceString
    
                    
                    If f = 0 Or f Is Nothing Then
                        r = False
                    Else
                        r = True
                    End If
                End Using
    The query without the trace works fine, but when I add:

    Code:
     sqlString = TryCast(f, System.Data.Objects.ObjectQuery).ToTraceString
    I get an error: Object reference not set to an instance of an object. is there another way I should be tracing this?


    Thanks.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to trace sql generated by Entity Framework query using singleordefault in vs2

    You're calling ToTraceString on this:
    Code:
    (From c In ce.MYTABLEs
    Where c.IYEAR = iy And c.ISEQUENCE = isq
    Select c.IS_PRINTED).SingleOrDefault
    That's wrong. What's the return type of SingleOrDefault? Is it ObjectQuery or anything derived from ObjectQuery? No it's not, so you can call a member or extension of ObjectQuery on it. You have to call ToTraceString on the actual ObjectQuery, i.e. this bit:
    Code:
    (From c In ce.MYTABLEs
    Where c.IYEAR = iy And c.ISEQUENCE = isq
    Select c.IS_PRINTED)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width