efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Projection with dependent creates extra fields in SQL SELECT statement

Open BenjiZombie opened this issue 9 years ago • 2 comments

Hi,

Using the following code, I was expecting to see a more concise SQL query that includes only the projected fields, but instead the SQL query includes all fields:

var customer = _customers
	.AsNoTracking()
	.Select(e => new { e.Id, e.Referral, e.State, e.HomeAddress.City })
	.ToList();
info: Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory[1]
      Executed DbCommand (6ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT "e"."Id", "e"."CreatedBy", "e"."CreatedOn", "e"."Referral", "e"."RowVersion", "e"."State", "e"."UpdatedBy", "e"."UpdatedOn", "e.HomeAddress"."Id", "e.HomeAddress"."BillingAddressCustomerId", "e.HomeAddress"."City", "e.HomeAddress"."Country", "e.HomeAddress"."FormattedAddress", "e.HomeAddress"."HomeAddressCustomerId", "e.HomeAddress"."PostalCode", "e.HomeAddress"."Province", "e.HomeAddress"."StreetLine1", "e.HomeAddress"."StreetLine2"
      FROM "Application"."Customers" AS "e"
      LEFT JOIN "Application"."CustomerAddresses" AS "e.HomeAddress" ON "e"."Id" = "e.HomeAddress"."HomeAddressCustomerId"
      ORDER BY "e"."Id"

As you can guess, the Customer entity has a child HomeAddress entity navigation (one-to-one). If I omit the e.HomeAddress.City from the projection, the resulting SQL query is correct (as in, it only includes the Id, Referral and State fields in the SELECT statement).

Are my expectations incorrect?

Thanks

Further technical details

EF Core version: 1.1.2 Database Provider: NpgSql Operating system: Windows 10 IDE: Visual Studio 2017

BenjiZombie avatar Jul 06 '17 15:07 BenjiZombie

dupe of #6647 Can you try with 2.0.0-preview1 (or preview2) package?

smitpatel avatar Jul 06 '17 16:07 smitpatel

Unfortunately I cannot.

Thanks for the reference to the similar issue, I will try to work something out or wait for final 2.0.

Regards

BenjiZombie avatar Jul 06 '17 17:07 BenjiZombie