Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 12, 2011 01:22 AM by Decker Dong - MSFT
Apr 09, 2011 07:22 AM|LINK
I wonder how to trace genrated SQL like DataContext in LinqToSql.
I also read articles about the solution of EFProviderWrapper on Jaroslaw Kowalski's blog, but it is based on ObjectContext, does not work for DbContext.
Anyone know how to do this in DbContext?
Apr 09, 2011 08:47 AM|LINK
You can just use the ToString method:
string sql = (from t in MyDbContext.MyDbSet select t).ToString();
Apr 09, 2011 09:08 AM|LINK
Yes，I know this.
What I'm looking for is a solution to trace(log) all SQL statements .
Just like DataContxt.Log in Linq To Sql, it is very simple. But EF dont have the built-in function.
Apr 09, 2011 11:04 PM|LINK
Ok, I see. Well, there's a ObjectContext underlying the DbContext. Try something like this:
var MyObjectContext = ((IObjectContextAdapter)MyDbContext).ObjectContext;
Apr 10, 2011 12:24 AM|LINK
1) I'm using SQL Server Express. It does not have SQL Profile, but I find a open souce AnjLab SQLProfile, it's quite cool.
2) I hope to find a DIY solution. As you mentioned, I tried EFTracingProvider, but when I use a EFTracingConnection passing to he constructor of DbContext, it throws Exception . And I don't know how to use the internal ObjectContext to attach the function
of tracing after it has been created.
Apr 10, 2011 04:39 AM|LINK
Sorry, I don't really have any experience with EFTracingProvider, but here are some thoughts.
1. Would it be possible to use the DbContext contructor overload that accepts an ObjectContext parameter? Here's a link:
2. Here's a blog post by Kowalski that discusses logging in Entity Framework/Code First. Is this what your looking for?
3. Finally, what about overriding the DbContext.SaveChanges method or using the DbContext.SavingChanges event to do your own sql tracing?
Apr 10, 2011 04:40 AM|LINK
One final thought. Have you considered contacting Wokalski directly with your question?
Apr 12, 2011 01:22 AM|LINK
In the pure EF, I think you can try this：）
Suppose "q" is the result gotten from EntityFramework datasource e.master
var q = from m