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

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

Question

I have an ASP.NET MVC solution built on Entity Framework with Microsoft SQL Server 2008. I need to create a function that lets my users upload files.

What I would like is:

  • A solution that uses the Entity Framework to store files in the Database
  • A solution that detects and prevents from uploading the same file twice via some kind of hash/checksum
  • Tips on database/table design
1
17
2/18/2010 2:48:33 PM

Accepted Answer

The "right" way to store a file in a SQL Server 2008 database is to use the FILESTREAM data type. I'm not aware that the Entity Framework supports that, but you can certainly try and see what happens.

That said, most of the time when people do this, they don't store the file in the database. Doing so means that you need to go through ASP.NET and the database server just to serve a file which you could be serving directly from the web server. It can also somewhat complicate the backup picture for your database and site. So when we upload files to our MVC/Entity Framework, we store only a reference to the file location in the database, and store the file itself elsewhere.

Obviously, which strategy is right for you depends a lot on the particulars of your application.

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

Popular Answer

In your entity model, map the BLOB database column to a byte[] property. Assign the content of the uploaded file to that property of the entity object, and save changes in the ObjectContext.

To compute a hash, you can use the MD5CryptoServiceProvider class



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