Is it possible to eager load one property of a type without loading all it's properties?
Real life situation:
I have two entities, MediaLink and MediaItem.
My media item looks like this:
public partial class MediaItem
{
public MediaItem()
{
this.MediaLink = new HashSet<MediaLink>();
}
public int Id { get; set; }
public byte[] Content { get; set; }
public int Length { get; set; }
public string MimeType { get; set; }
public virtual ICollection<MediaLink> MediaLink { get; set; }
}
When writing a query, I need to access the MimeType
of the MediaItem
from the MediaLink
entity.
Yet when I use the navigation property to access the MimeType, the query also returns the Content
which on a video could be >10MB and cripples the performance.
I suspect this is not possible, but is there any way I can return the MimeType property of a MediaItem without it's content via EF?
EDIT: Thanks for the comments, I should probably add that I think I could do this with a standard linq query by projecting the parent MediaLink
and the property I want from MediaItem
into a new type but I'm needing to do this inside and AutoMapper mapping.
At the moment, the below mapping requires me to go into the MediaItem
via the navigation property:
Mapper.CreateMap<MediaLink, MediaModel>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.MediaTypeId))
.ForMember(dest => dest.MimeType, opt => opt.MapFrom(src => src.MediaItem.MimeType));
What I really want to do is something like this:
Mapper.CreateMap<MediaLink, MediaModel>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.MediaTypeId))
.ForMember(dest => dest.MimeType, opt => opt.MapFrom(src => (from mi in src.MediaItem select mi.MimeType)));
And hope the projection would return the whole MediaItem
entity.