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.
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.
Miniprofiler a free alternative to entity framework profiler that will allow you to trace all sql queries made during web requests
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
I'll just leave it here.
public class Context : DbContext
{
public Context(string connectionString) : base(connectionString)
{ Database.Log = Console.Write; }
}
Related
In a asp.net mvc project, there are various ways to logging dml operations like insert, update, delete. I wonder which one is useful using aspects in code or using triggers or CDC in database server or are there more logical ways?
There's quite a few ways.
Assuming you're using Entity Framework as your ORM, you could register an application interceptor (see IDbCommandInterceptor). See Logging and intercepting database operations for more info...
Another approach, which is quite handy to log ALL database operations is to set the Database.Log property on your DbContext implementation as follows:
public class YourContext : DbContext
{
public YourContext(ILogger<YourContext> logger)
{
Database.Log = sql => logger.LogDebug(sql);
}
}
^^ I found this approach to be quite useful for debugging. I would have a file appender/target/sink (depending on what logging framework you're using e.g. NLog/Log4net/Serilog) specifically for my DbContext implementation, so that I could easily see the SQL of the database operations that are being executed without any noise from other application logs.
The other approach that I've used quite a bit is to use SQL Server Profiler, but this assumes a) you're using SQL Server, and b) you're able to connect to the database server.
I'm using Entity Framework to generate sql queries. And DB admins send me queries which are too slow or not optimized. But in app are hundreds requests to DB and too hard to find right one.
Is it any possibilities to set label in query? I need it to find query faster than I do it now.
depends a little on your EF version ...
in EF6 for example you could take the command interception approach to modify all SQL statements EF generates ...
see https://www.entityframeworktutorial.net/entityframework6/database-command-interception.aspx
Very strange situation. I am using EF 6.1 in a project along with my friend. He told that when he sets a breakpoint on return from this method
private static List<Tag> PreloadTags(ref ApplicationDbContext db)
{
var enumerableResult = db.Tags.OrderBy(x => Guid.NewGuid()).Take(PrefetchSize*TagsPerPicture);
return enumerableResult.ToList();
}
and howevers over the enumerableResult he sees the actual SQL query used to query the SQL server. However (and this is the anomaly), when I do this, I do not see the query. I only see the type System.Data.Entity.Infrastructure.DbQuery. I am using VS2013 Professional, he is using VS2013 Web Express. Any ideas? It is a great mystery to me although I am sure there is reasonable explanation.
Screenshot of him seeing the query.
Screenshot of me NOT seeing the query.
Entity Framework is fine but some queries are sub-optimal. Can one write some SQL queries by hand or as MS SQL Server 2008 R2 views, execute this selects and then associate somehow entities with them? I examined some generated SQL and they are quite ugly...
Question 2: Is MS SQL Server 2008 R2 caching queries? I'd like to have strong caching, how can I tune it? (with MySQL it's so simple).
You have a lot of options available to you. You can use .ExecuteStoreCommand() in EF 4.1 to execute SQL against the database, or you can map stored procedures into EF as well. Furthermore, you can map views as entities in EF if that's necessary.
SQL Server has very sophisticated caching mechanisms for data pages as well as execution plans , and it's pretty much automatic to the developer. I would suggest posting a question with concrete example with the operations you want to optimize.
LINQ-to-SQL had several ways, including a visualizer add-in, to view the generated SQL from an IQueryable.
I can't find the equivalent for Entity Framework 4. Nothing on StackOverflow, no blogs. How is it done?
Preferably, I'd like to be able to do it in code and without having to actually execute the query just to see it.
Thanks!
there are several approaches to looking at the sql.
Free
On the ObjectQuery do .ToTraceString() that will show u the sql generated for the query.
Download ef tracing provider written by one of the EF team members. EF Tracing Provider
Linq To Entities visualizer which you can download here.
LinqPad
Sqlserver profiler
Commercial
Efprof.com
If you can't get any of the other solutions to work, you could try using the SQL Server Profiler if you have access to the SQL Machine.
Within SQL Server Management Studio you can do the following:
Tools -> SQL Server Profiler.
Create a new Trace and run your code and you should see the queries come across. You can create some filters so you don't see the security / audit stuff which you probably don't care about.
Hi there is this visualizer...but I could not get it to work for me...you could try it..I would recommend LINQPad for viewing your queries you can setup your ef connection and execute your queries.