2

I have a function to save product into db. How can I get the sql statement for SaveChanges()?

I want the query, so that I can store it in the db for database changes logging.

public void SaveProduct(Product product)
        {
            if (product.ProductID == 0)
            {
                context.Products.Add(product);
            }
            else
            {
                Product dbEntry = context.Products.Find(product.ProductID);
                if (dbEntry != null)
                {
                    dbEntry.Name = product.Name;
                    dbEntry.Description = product.Description;
                    dbEntry.Price = product.Price;
                    dbEntry.Category = product.Category;
                }
            }
            context.SaveChanges();
        }
    }
Alvin
  • 8,219
  • 25
  • 96
  • 177
  • What I would like to do is to have sql statement save into database. – Alvin Jun 15 '13 at 01:50
  • Is it able to do like var strSql = (ObjectQuery)dbEntry.ToTraceString(); ? – Alvin Jun 15 '13 at 01:57
  • If you want to get the string in C#, see the answer by [*Marty Dill*](http://stackoverflow.com/a/10568201/1715579). If you only need to capture it in SQL, maybe you should also look for a pure SQL solution. – p.s.w.g Jun 15 '13 at 02:00

0 Answers0