Entity Framework data type for document storage in SQL Server 2008.

document entity-framework sql-server-2008 store types


I'm trying to store a document in SQL Server 2008 using the Entity Framework.

I believe I have the code for doing this completed. The problem I'm now facing is which Data Type to use in SQL Server and in my entity model.

'Image' was my first choice, but this causes an "Invalid mapping" error when I update the model. I see that there's no equivalent of 'Image' (going by the Type drop-down in the entity's properties).

So then I tried 'varbinary(MAX)' and I see that this maps to 'binary' in the entity model. However, when I run the code it tells me that the data would be truncated so it stopped. Upon investigation I see that the SQL Server Data Type 'binary' is 8000 bytes long - which is why I chose 'varbinary(MAX)' - so the entity model seems to be reducing/mapping 'varbinary(MAX)' to 'binary'.

Is this right?

If so, what should my Data Types be (in both SQL Server 2008 and in my entity model) please? Any suggestions?

11/11/2010 8:49:27 PM

Accepted Answer

Change the data type of your model to byte[] and it will be all right dude, if you need more explanations please leave a comment.


Dude, I had tried it before in Linq to Sql and this time I tried it in EF, in conceptual model of your namely Foo.edmx file your type is Binary(you can open it through open with context menu of Visual Studio and then selecting Xml Editor or any other text editor like notepad) but in a generated file named Foo.designer.cs your data type is Byte[].

And there is not a limit you mentioned above. I tried it with a 10000 bytes and it's inserted successfully without truncating my array. About benchmarking on saving documents in database or file system I read an article and it said that in Sql Server 7, file system have a better performance on retrieving stored data but in later versions of Sql Server, it take over the file system speed and it suggested saving documents on Sql Server.

IMHO on saving documents, if they are not too large, I prefer to store them on DB (NoSql DBs has great performance here as far as I know),

First: Integrity of my data,

Second: More performance that you can have(cause if your folder has large number of files, reading and writing files in those folder slows down gradually more and more unless you organize them in more than one folder and preferably in a tree like folders),

Third: Security policies that you may apply to them through your application more easily(although you can do this on file system approach but i think it's easier here)

Fourth: you can benefit from facilities provided by your DBMS for querying and manipulating and ... those files

and much more ... :-)

11/13/2010 5:50:28 PM

Popular Answer

Ideally you should not store documents in the database, instead store the path to the document in the database, which then points to the physical document on the web server itself (or some other storage, CDN, etc).

However, if you must store it in SQL Server (and seeing as though your on SQL 2008), you should be using FILESTREAM.

And it is supported by EF4 (i believe). It maps to binary.

As i said though, i'm not sure how well this will perform, run some benchmarks - and if it's not performing too well, try using regular ADO.NET/FileStream API.

I still think you should put it on the file system, not in the database (my opinion of course)

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow