0

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.

dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89
  • 1
    don't think so, you need to define separate entity, that holds *only* information you care about. – Tigran Nov 07 '13 at 10:28
  • 1
    check this : http://stackoverflow.com/questions/3274241/entity-framework-4-selective-lazy-loading-properties – Pranay Rana Nov 07 '13 at 10:45
  • What does `MediaLink` look like? Couldn't you just store the MimeType along with the link for quick reference? – James Nov 07 '13 at 11:30
  • That's weird, co-developer called James in the office just suggested that! Yeah I think I'm going to end up going down that route, either that or create a new 'MediaContent' type which just contains the content and hangs from 'MediaItem' – dougajmcdonald Nov 07 '13 at 12:36

0 Answers0