title: Entity Framework and AutoMapper - query returns all columns for view model and is too slow
body: <p>My repository layer used to return a ViewModel directly e.g.</p>
  
  <pre><code>public IEnumerable&lt;CommentViewModel&gt; GetComments()
  {
     return from c in context.Comments
                   select new CommentViewModel
                   {
                      FirstName = c.FirstName,
                      Comment = c.Comment
                   };
  }
  </code></pre>
  
  <p>I then read that the repository should not return view models so I changed it to:</p>
  
  <pre><code>public IEnumerable&lt;Comment&gt; GetComments()
  {
     return from c in context.Comments;
  }
  </code></pre>
  
  <p>In the service layer I then changed:</p>
  
  <pre><code>public IEnumerable&lt;CommentViewModel&gt; GetComments(int postId)
  {
     return _repository.GetComments();
  }
  </code></pre>
  
  <p>To:</p>
  
  <pre><code>public IEnumerable&lt;CommentViewModel&gt; GetComments()
  {
     var comments = _repository.GetComments();
  
     return Mapper.Map&lt;IEnumerable&lt;Comment&gt;, IEnumerable&lt;CommentViewModel&gt;&gt;(comments);
  }
  </code></pre>
  
  <p>The problem is the queries used to take 7ms and now take 85ms!</p>
  
  <p>Where am I going wrong?</p>
  
  <p>The query generated used to be (top 5 and order by is done in controller):</p>
  
  <pre><code>SELECT TOP (5) [t0].[FirstName], [t0].[Comment]
  FROM [dbo].[Comment] AS [t0]
  ORDER BY [t0].[CreateDate] DESC 
  </code></pre>
  
  <p>It's now returning all columns:</p>
  
  <pre><code>SELECT 
  [Extent1].[Id] AS [Id], 
  [Extent1].[FirstName] AS [FirstName], 
  [Extent1].[LastName] AS [LastName], 
  [Extent1].[DatePosted] AS [DatePosted], 
  [Extent1].[Comment] AS [Comment],
  [Extent1].[IPAddress] AS [IPAddress]
  FROM [dbo].[Comment] AS [Extent1]
  </code></pre>