Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL FileStream + Entity Framework store large files

When i want to store a file in a filestream column, i always need to read the whole binary into the memory:

using (MemoryStream memoryStream = new MemoryStream())
{
   sourceStream.CopyTo(memoryStream);
   binaryStore.Content = memoryStream.ToArray(); //Content = filestream column
}

is there a way with the entity framework, to put the stream directly? Because, if i want to upload a large file, i get a OutOfMemoryException.

like image 822
Kaffee Bohne Avatar asked Apr 07 '12 14:04

Kaffee Bohne


2 Answers

EF doesn't have any support for FIlESTREAM. It handles all interactions with FILESTREAM as normal VARBINARY(MAX) column so if you want to use streaming you must use ADO.NET directly.

like image 65
Ladislav Mrnka Avatar answered Oct 22 '22 15:10

Ladislav Mrnka


Didn't see any update about FILESTREAM support in EF. (Mentioned before as partial support with .net 3.5 sp1 release here). I suppose entity framework is accessing FILESTREAM through TSQL and apparent that you will not be able to get the streaming performance benefits of FILESTREAM. (need to read all file content into memory)

So, the recommended approach is using with SqlFileStream .Net API.

http://lennilobel.wordpress.com/2011/08/22/using-sqlfilestream-in-c-to-access-sql-server-filestream-data/

like image 2
Min Min Avatar answered Oct 22 '22 17:10

Min Min