none
SQL Queries of entity framework RRS feed

  • Question

  • Hi,

    how to get the sql queries of entity framework operations insert update delete select 

    is it possible to show that in the debug output window in vb.net

    please help 

    thanks in advance.....


    Tuesday, May 26, 2015 4:58 PM

Answers

All replies

  • See

    Database.Log

    eg

    using (var db = new MyDbContext())
    {
       db.Log = (s) => Console.WriteLine(s);
       . . .
    
    }

    David


    David http://blogs.msdn.com/b/dbrowne/

    Tuesday, May 26, 2015 5:35 PM
  • Hi David,

    i used ur code in vb.net 

     Using context As New EmployeeModelContext(connectionstring)
                context.Database.Log = Function(s) Console.WriteLine(s)

    End Using

    but i am getting error Expression does not produce a value at console.writeline(s).

    my code

    Using context As New EmployeeModelContext(connectionString)
    
        Dim model As Employee = (From item In context.Employees Where item.EmployeeId = 1 Select item).FirstOrDefault
    
        context.Set(Of Employee).Remove(model)
    
        context.SaveChanges()
    
    End Using
    i want to see the queries of select and delete in the above code. i want to get the every action happen to db from entity framework


    Friday, May 29, 2015 8:49 AM
  • This should be

    Using context As New EmployeeModelContext(connectionstring)
                 context.Database.Log = Sub(s) Console.WriteLine(s) 
    End Using
    or

            Using db As New Model1
                db.Database.Log = AddressOf WriteLog
    
    
               . . .       
            End Using
    
        End Sub
    
        Sub WriteLog(s As String)
            Console.WriteLine(s)
        End Sub

    David


    David http://blogs.msdn.com/b/dbrowne/

    Friday, May 29, 2015 1:14 PM
  • Using context As New EmployeeModelContext(connectionString)
    
    
                context.Database.Log = AddressOf WriteLog
    
                Dim model As Employee = (From item In context.Employees Where item.EmpId= 1 Select item).FirstOrDefault
     
                context.Set(Of Employee).Remove(model)
    
                context.SaveChanges()
    
            End Using


     Shared Sub WriteLog(s As String)
            Debug.WriteLine(s)
        End Sub

    Thank you very much David,

    this code is working 

     i am getting queries but it is showing many unwanted queries like INFORMATION_SCHEMA.TABLES, [dbo].[__MigrationHistory], etc queries and other log informations. 

     is it not possible to show only this employee select query and this remove query. 



    Friday, May 29, 2015 4:18 PM