6

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

Tr1stan
  • 2,755
  • 1
  • 26
  • 45
  • Can you post the code you are running? – Arthur Vickers Mar 14 '12 at 04:58
  • @ajcvickers The system I'm dealing with is rather large, so supplying the code as-is won't be possible, it will take me some time to get a prototype together. I take it that this is not what you'd expect either then? – Tr1stan Mar 14 '12 at 17:22
  • Every time a LINQ to Entities query is enumerated (e.g. with ToList()), then the query is executed against the database. I was wondering if it was this. – Arthur Vickers Mar 14 '12 at 18:33
  • are you binding it to a grid? I found when I bind a grid to a entity query without a .tolist() then it is executed several times. – Ezi Mar 23 '12 at 02:31
  • I'm not using a grid (I assume you mean a .net Data Grid or similar) - My presentation layer is built using .net MVC and I'm going through a Service Layer, which in tern uses a Repository. From your questions, it sounds as though you agree with me when you think my queries might be running twice - I'll do some serious investigation now.. – Tr1stan Mar 23 '12 at 14:18

1 Answers1

5

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.

Tr1stan
  • 2,755
  • 1
  • 26
  • 45