Reputation: 81
I was wondering if there is any kind of lazy loading for a single attribute to increase the loading speed. I'm working with entity framwork 6.1.3 and the following model:
public class Photo : HoHoRecord
{
public string Description { get; set; }
public int? GalleryId { get; set; }
public virtual Gallery Gallery { get; set; }
public byte[] Thumbnail { get; set; }
public byte[] Content { get; set; }
public virtual BitmapImage ThumbnailBitmapImage
{
get
{
return ImageByteConverter.GetBitmapFromContentByteArray(Thumbnail);
}
}
public virtual BitmapImage ContentBitmapImage
{
get
{
return ImageByteConverter.GetBitmapFromContentByteArray(Content);
}
}
}
If I access the list of Photos in another model, I would prefer, that the attribute Photo.Content is not download yet. It should be downloaded only, when I access the property Photo.ContentBitmapImage.
This way I would be able increase the main loading speed of the whole liste in a good way.
Is there any way to achive this without creating an additional model class to store the byte array?
Thansk and regards
Markus
Upvotes: 0
Views: 433
Reputation: 81593
The answer is no.
However there is a couple of things you could consider.
Projecting to an intermediate class so it doesn't drag the images over all the time. This will save load times. However you lose the ability to use it as an attached entity and so forth.
Vertically partitioning the table so your images are not part of the entity it self and in another table/entity. Or creating a dedicated images table, both these will give you true lazy loadable images.
The other thing to consider here is, that there are times when images are best file backed and not part of a Database. There are pros and cons and reams of threads written about this topic, and it comes down to your needs and how big the images are
In regards to the first point, you could easily make an extensions/methods to fetch the image when needed
Upvotes: 1