Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is my Entity Framework generated SQL executing twice?

I'm looking at analysing the performance of the SQL generated from Entity Framework 1, using MS SQL 2008.

When I run a trace in SQL Server Profiler 2008, I noticed something that I didn't expect. For each query that gets executed, I get two RPC:Completed statements, separated by an exec sp_reset_connection statement. Is this the expected behavior?

SQL Server Profiler Results

like image 518
Tr1stan Avatar asked Mar 06 '12 12:03

Tr1stan


People also ask

How do I see SQL query generated by Entity Framework Core?

Visual Studio Output NET Core make it pretty easy to output the SQL when debugging. You can set the logging level for Microsoft to “Information”. Then you can view the SQL in the output log when running in debug mode from Visual Studio. The SQL will then be visible in the Output panel.

Does Entity Framework use SQL?

EF Core works with many databases, including SQL Database (on-premises and Azure), SQLite, MySQL, PostgreSQL, and Azure Cosmos DB.


1 Answers

Answer: Yes

It turns out that it was a "feature" within AutoMapper that was causing my issue.

See here: When Mapping an IQueryable I see the database getting hit twice in my profiler.

Unfortunately because I'm using version 1.1 (.net 3.5) this doesn't look like it's going to get fixed.

Solution: call .ToList() on the IQueryable object before passing it into the Mapper.Map() method. Allowing the Mapper to enumerate the object causes a double execution.

like image 147
Tr1stan Avatar answered Sep 22 '22 23:09

Tr1stan