With entity framework, you may save files to a database.

.net asp.net-mvc c# entity-framework upload

Question

I have an Entity Framework-based ASP.NET MVC application with Microsoft SQL Server 2008 installed. I must develop a feature that enables file uploading by my users.

What I'm looking for is:

  • a technique that stores files in the database using Entity Framework
  • a method for detecting duplicate uploads of the same file and stopping them using a hash or checksum
  • Tips for designing databases and tables
1
17
2/18/2010 2:48:33 PM

Accepted Answer

In a SQL Server 2008 database, the "correct" approach to store a file is to utilize the data type FILESTREAM. Although the Entity Framework doesn't seem to allow it, you may absolutely give it a go and see what occurs.

However, the majority of the time when this is done, the file is not saved to the database. By doing this, you are forcing a file that might be served straight from the web server to pass via ASP.NET and the database server. Additionally, it can make your database and site's backup situation a little more complicated. Because of this, when files are uploaded to our MVC/Entity Framework, we merely retain a reference to the file location in the database and keep the actual file somewhere else.

Obviously, the specifics of your application will have a big impact on which technique is best for you.

9
2/18/2010 2:02:57 PM

Popular Answer

Map the BLOB database field to an in your entity model.byte[] property. Assign the uploaded file's content to that entity object's property, then save your modifications.ObjectContext .

The MD5CryptoServiceProvider class may be used to calculate hashes.



Related Questions





Related

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