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.
Ok, I see. Well, there's a ObjectContext underlying the DbContext. Try something like this:
var MyObjectContext = ((IObjectContextAdapter)MyDbContext).ObjectContext;
Then you should be able to use the EF tracing you mentioned in your orignal post. Just as a side note, is there any reason why you're not using SQL Profiler instead?
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.
ChanceWen
Member
7 Points
8 Posts
In EF 4.1 DbContext how to trace genrated SQL
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?
Thank you.
chohmann
Star
9385 Points
1644 Posts
Re: In EF 4.1 DbContext how to trace genrated SQL
Apr 09, 2011 08:47 AM|LINK
You can just use the ToString method:
ChanceWen
Member
7 Points
8 Posts
Re: In EF 4.1 DbContext how to trace genrated SQL
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.
chohmann
Star
9385 Points
1644 Posts
Re: In EF 4.1 DbContext how to trace genrated SQL
Apr 09, 2011 11:04 PM|LINK
Ok, I see. Well, there's a ObjectContext underlying the DbContext. Try something like this:
Then you should be able to use the EF tracing you mentioned in your orignal post. Just as a side note, is there any reason why you're not using SQL Profiler instead?ChanceWen
Member
7 Points
8 Posts
Re: In EF 4.1 DbContext how to trace genrated SQL
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.
Thank you!
chohmann
Star
9385 Points
1644 Posts
Re: In EF 4.1 DbContext how to trace genrated SQL
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:
http://msdn.microsoft.com/en-us/library/gg696505%28v=VS.103%29.aspx
2. Here's a blog post by Kowalski that discusses logging in Entity Framework/Code First. Is this what your looking for?
http://jkowalski.com/2010/04/23/logging-sql-statements-in-entity-frameworkcode-first/
3. Finally, what about overriding the DbContext.SaveChanges method or using the DbContext.SavingChanges event to do your own sql tracing?
chohmann
Star
9385 Points
1644 Posts
Re: In EF 4.1 DbContext how to trace genrated SQL
Apr 10, 2011 04:40 AM|LINK
One final thought. Have you considered contacting Wokalski directly with your question?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: In EF 4.1 DbContext how to trace genrated SQL
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
<div class="container"> <div class="line number1 index0 alt2">var q = from mine.master</div> <div class="line number2 index1 alt1">select m.t.Length;</div> </div> <div class="line number1 index0 alt2">Console.WriteLine(((ObjectQuery)q).ToTraceString());</div> <div class="line number1 index0 alt2"></div> <div class="line number1 index0 alt2">Suppoi</div>