Entity Framework selects too many columns in views

Viewed 684

When I use Entity Framework against a SQL table, it only refers to the necessary columns in the generated SQL:

ctx.Types.Select(rdi => rdi.Name)

becomes

SELECT [Extent1].[Name] AS [Name]
FROM [dbo].[Types] AS [Extent1]

However, if I make an analogous query against a SQL view, Entity Framework generates SQL referring to every column in the view:

ViewTypes.Select(rdi => rdi.Name)

becomes

SELECT [Extent1].[Name] AS [Name]
FROM (SELECT 
      [ViewTypes].[Name] AS [Name], 
      ... every other column in my view ...
      FROM [dbo].[ViewReferenceDataTypes] AS [ViewReferenceDataTypes]) AS [Extent1]

I'm sure SQL Server will perform its own optimization to end up ignoring all the columns it doesn't care about, but this still results in a massive block of SQL being sent to the server. (My actual example included a join, which resulted in every column from several tables being selected...)

Is there a good reason for Entity Framework to do this? Is there a way to make it not do this?

1 Answers
Related