Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent Entity Framework from loading a FileStream column into a byte array?

I am developing a file storage application, and we have incorporated the FileStream type in our database. The system is expected to support large files. One portion of the application allows for bulk uploads of multiple documents. These documents then have to be linked to other entities within the system.

One page is designed to show unlinked documents, to allow a user to link the documents one at a time to entities. After doing some load testing of the upload process, we found that the memory footprint of the ASP.NET worker process spiked to over a GB when loading this Unlinked Documents page.

After investigation, it seems that Entity Framework is loading the entire document row entity (including the FileStream, converted to a byte array) for hundreds of unlinked documents. In my repository class, I don't save this byte array when converting to a Model, but by then it's too late. EF has spent the time and the memory to allocate the byte array into the Repository class representation.

Is there a way that I can tell EF not to load this byte array unless I explicitly ask for it?

like image 749
Garrison Neely Avatar asked Apr 19 '12 21:04

Garrison Neely


1 Answers

Garrison, you can use a feature called "entity splitting" where you map two related entities to a single table. So in the first entity you would have all but the filestream property. In the 2nd entity you would have the primary key (same as in the first entity) and the filestream property. Create a one:one relationship between the entities in your model and then map both entities to the same table. Now the entity with the filestream is related and you can load or lazy load it as needed. The only downside is that it's not a property of your main entity, so you have to navigate to the related entity and then the property.

myEntity.MyRelatedEntity.TheFileStreamProperty

Here's an old blog post I wrote that shows how to do this in the EDM Designer.

You can also achieve the same mapping with Code FIrst. Here's an MSDN article I wrote that contains an entity splitting article. http://msdn.microsoft.com/en-us/data/hh272551

HTH Julie

like image 152
Julie Lerman Avatar answered Oct 03 '22 06:10

Julie Lerman