This question already has an answer here:
According this thread, we can log the generated SQL
via EF
, but what about DbContext.SaveChanges()
? Is there any easy way to do this job without any extra frameworks?
This question already has an answer here:
According this thread, we can log the generated SQL
via EF
, but what about DbContext.SaveChanges()
? Is there any easy way to do this job without any extra frameworks?
This should help, the EFTracingProvider
http://code.msdn.microsoft.com/EFProviderWrappers
See http://www.codeproject.com/Articles/499902/Profiling-Entity-Framework-5-in-code. I implemented Mr. Cook's idea in an asp.net mvc application using a Code First, POCO DbContext, Entity Framework 5.
The context class for the application derives from DbContext:
The constructor for the context hooks up the SavingChanges event (I only want to do the expensive reflection for debug builds):
The saving changes event writes the generated sql to the output window. The code I copied from Mr. Cook converts the DbParameter to a SqlParamter, which I leave as-is because I'm hitting a Sql Server, but I'm assuming that conversion would fail if you are hitting some other kind of database.