none
How to see EF SaveChanges() generate what sql RRS feed

  • Question

  • i am interested to see what sql is generated by

    EF SaveChanges()

    . so i search google and found one easy trick to do it.

    i create class

    public class MyLogger { public static void Log(string component, string message) { Console.WriteLine("Component: {0} Message: {1} ", component, message); } }

    also hook log function this way

        using (var db = new TestDBContext())
        {
            db.Database.Log = s => MyLogger.Log("EFApp", s);
    
    var existingCustomer = db.Customer
    .Include(a => a.Addresses.Select(x => x.Contacts))
    .FirstOrDefault(p => p.CustomerID == 5);
    
            existingCustomer.FirstName = "Test Customer123";
            db.SaveChanges();
        }  

    and saw the partial insert sql statement generated as below

    INSERT [dbo].[Addresses]([Address1], [Address2], [IsDefault], [SerialNo], [CustomerID])
    VALUES (@0, @1, @2, @3, @4)
    SELECT [AddressID]
    FROM [dbo].[Addresses]
    WHERE @@ROWCOUNT > 0 AND [AddressID] = scope_identity()

    why i said partial because there was no value. so please tell me how could see insert or update statement generated by

    SaveChanges()

    function with proper values.

    thanks

    Friday, November 4, 2016 12:44 PM

Answers