18

I'm using Entity Framework Code First and I'd like to be able to record all the SQL queries generated by the DbContext. In Linq to sql there was a DB log and I can't seem to find it in EF. I could then drop them in a log or output it to the page.

I'm using the version 4.1.0.0 of the EntityFramework Assembly.

6
  • Does EF allow you to pass in a connection (rather than a string)? I know of ways if you can May 29, 2011 at 15:33
  • (to put that in context, we do this tracing in all of the stackoverflow code, and we don't use .Log) May 29, 2011 at 15:41
  • Marc do you use a custom Connection implementation that hooks up the logging? May 29, 2011 at 16:24
  • yes - custom connection, command, reader, etc - and in the process of open-sourcing it May 29, 2011 at 19:17
  • @Marc Gravel Let me know when you open source it I'd really appreciate it. May 29, 2011 at 20:37

4 Answers 4

6

Your best bet would be to use the Entity Framework Profiler, although it's unfortunately not free.

You can also manually get the SQL it will generate by running a ToString() on the IQueryable itself, but that will have to be done on a per-query basis.

One final option is that if you are using MS Sql Server as your backend, you can load up the Sql Server Profiler (that comes with Sql Server Management Studio I believe) and log the sql statements from there.

4
  • 1
    The OP is in luck if he decides to purchase this profiler as the creator (Ayende) is getting married today and offering a 29% discount until June 1st. ayende.com/blog/8193/the-big-event-is-just-a-week-away
    – Mike
    May 29, 2011 at 15:45
  • Well crap, I might have to finally purchase it now as well. $100 off is a bit hard to turn down
    – KallDrexx
    May 29, 2011 at 15:52
  • 1
    I know about profiler tools. I would like an in code solution that will give me output on the MVC web page itself. I dont want to have to do it per query I would like a event on the context that will let me capture the query and log it. May 29, 2011 at 16:22
  • @feanz - the code we use here does exactly that; it allows us to review the SQL live, in context, on every single request May 29, 2011 at 19:34
1

Miniprofiler a free alternative to entity framework profiler that will allow you to trace all sql queries made during web requests

0

With tools like EF it becomes more important than ever to use the SQL Server Profiler, and it should be the primary tool used for this type of situations, if it was important when we actually wrote the queries it is even more important now that these tools build the queries for us, it's a must not only for debugging but also for optimization

0
0

I'll just leave it here.

public class Context : DbContext
{
    public Context(string connectionString) : base(connectionString)
    { Database.Log = Console.Write;  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.