Select only specific columns from included table in Entity Framework Core

entity-framework entity-framework-core

Question

If I have this

var selectedEntities = db.MyEntities.Include(item => item.RelatedEntities);

It will load all properties (columns) in MyEntities but also all properties in ReleatedEntities. If I only need one property from RelatedEntities, how would I specify that?

1
4
4/4/2018 12:22:50 PM

Popular Answer

I was looking for the same, and after referring to other questions it seems like it's not possible. The understanding i got is that an EF entity is represented by the collection of it's properties, and hence loading individual values will not fully define the Entity.

To load only selected properties, we need to use Select query, instead of loading the related data. The query will result in an anonymous type.

Note : If the resulting type contains any entity, then the changes will be tracked. Refer Tracking and projections

References :

1
7/6/2018 12:10:34 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow