View sql queries generated by EF Core - entity-framework

Is there anyway to see what SQL query EF Core is generating? (ASP .Net Core 1.1)

Just my opinion, but I believe the easiest, fastest and cleanest solution is to use SQL Profiler.
Just run the SQL Profiler that comes with SSMS, start the trace, execute the action that executes the query, pause the trace and check the exact query that was executed.
It's independent of the EF version you are using.

Related

Batching ExecuteSqlCommand statements on a code-first DbContext

I'm calling ExecuteSqlCommand several thousands times per minute and each one is a round-trip to the database. The command itself calls a stored procedure and the parameters vary from INTs to NVARCHARS to "Structured" table-value parameters. When I look at the SQL commands generated by Entity Framework via SQL Profiler quite a sizeable chunk of data is being sent to the DB.
Given that there is no return value from the SP, it's very much "fire and forget", I'd like to try to batch up the SQL commands and send, say, a batch of 10 at once. Is this possible using Entity Framework? Is there a way to get Entity Framework to return me the full SQL command per call and then I could concatenate the SQL myself and call ExecuteSqlCommand myself?
EntityFramework currently does not support batching. However if you are just executing SQL queries then I don't see a benefit of using EntityFramework. You can go one level below to pure ADO.NET where it is supported to send Sql commands in batches. Take a look at this MSDN post for more details: http://msdn.microsoft.com/en-us/library/aadf8fk2.aspx

Why entity framework need generate local query view? How about Nhibernate?

I found a very interesting in page: http://msdn.microsoft.com/en-us/library/cc853327.aspx
Here you can see during query stages, there is an stage named "Generating views" will cost a lot. Even though EF provides some method to pre-compile it, but if you have many query without pre-compile you still may get problems.
You can find How to: Pre-Generate Views to Improve Query Performance here: http://msdn.microsoft.com/en-us/library/bb896240.aspx
And here, you can see query without pre-generate will cost twice time. So that means it does cost a lot. http://blogs.msdn.com/b/appfabriccat/archive/2010/08/06/isolating-performance-with-precompiled-pre-generated-views-in-the-entity-framework-4.aspx
So I have a question, why EF design this stage? And does NHibernate also have this stage? If true, how about it performance in Nhibernate?
EF views have nothing to do with SQL views - EF views are mapping transformation compiled into executable code. EF use these transformation to convert its query representation into target SQL representation. The reason for this compilation is performance of the whole application - you need to invest time to initialization but all your subsequent queries and updates will use compiled code instead of some lookup in EDM. If you don't need to modify mapping at runtime you can even pre-compile those views during compilation of your application.
EF views are used for query preparation (transforming one representation into another) but the query preparation must be done for each unique query. In EF 4 this preparation is not cached unless you manually use Compiled query. In EF 4.5 and 5.0 (.NET 4.5) all queries are automatically "compiled" = there is the cache and each unique query is really prepared only once. Subsequent execution of the same query use compiled version from the cache.
You can read more about performance and EF 5.0 in this beginner guide.

Entity Framework see SQL sentence when calling stored procedure

Is there a way to see the underlying SQL sentence when executing a stored procedure in Entity Framework (3.5)?
To use the stored procedure I did from the diagram: Add, Function Import… etc
Thanks
UPDATE 1
I downloaded 'AnjLab Sql Profiler' from.
http://code.google.com/p/sqlexpressprofiler/downloads/list
And was able to see that the stored procedure is executed correctly.
You can use any type of database profiler - for example SQL profiler for SQL server or you can use either EFTracingProvider or any EF profiler (these tools are usually commercial). Here is whole article about these techniques.

How can I see what TSQL Entity Framework is generating for a query?

Imagine this linq query:
return db.Projects.Where(p => p.ProjectID == id);
How can I see what TSQL it's generating?
I use LINQPad
You can see your queries as lambda expressions or SQL
SQL Server profiler does exactly this. If you are on SQL Server Express, there is an open source alternative called AnjLab Sql Profiler since SQL server profiler isn't included with express.
You can attach the Sql Server Profiler to your database while running the query. Furthermore, have a look at the tracing and caching utility for EF.

DataContext.Log equivalent in Entity Framework?

In Link-2-SQL, I can use the DataContext.Log property to see the exact queries that are getting thrown to SQL Server.
Is there an equivalent to this in Entity Framework?
ObjectQuery.ToTraceString()
Since Entity Framework supports multiple backends (as opposed to Linq-to-SQL which is SQL Server only), you can't really get the actual SQL being sent to the backend server from EF.
In order to really see what's going on, I'd recommend firing up SQL Profiler on the SQL Server backend, and see what queries get sent its way.
See this article on Simple-Talk and possibly this video series on becoming a SQL Profiler master if you're not familiar with the SQL Profiler tool.
Marc