問題描述
我有一個基于 Entity Framework 和 Microsoft SQL Server 2008 的 ASP.NET MVC 解決方案.我需要創(chuàng)建一個函數(shù)讓我的用戶上傳文件.
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.
我想要的是:
- 使用實體框架將文件存儲在數(shù)據(jù)庫中的解決方案
- 一種通過某種哈希/校驗和檢測并防止兩次上傳同一文件的解決方案
- 數(shù)據(jù)庫/表設(shè)計技巧
推薦答案
對"在 SQL Server 2008 數(shù)據(jù)庫中存儲文件的方法是 使用 FILESTREAM 數(shù)據(jù)類型.我不知道實體框架是否支持這一點,但您當然可以嘗試看看會發(fā)生什么.
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.
也就是說,大多數(shù)時候人們這樣做時,他們不會將文件存儲在數(shù)據(jù)庫中.這樣做意味著您需要通過 ASP.NET 和數(shù)據(jù)庫服務(wù)器來提供可以直接從 Web 服務(wù)器提供的文件.它還可能使您的數(shù)據(jù)庫和站點的備份圖片變得有些復(fù)雜.因此,當我們將文件上傳到我們的 MVC/Entity Framework 時,我們只存儲對數(shù)據(jù)庫中文件位置的引用,并將文件本身存儲在其他地方.
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.
顯然,哪種策略適合您在很大程度上取決于您的應(yīng)用程序的細節(jié).
Obviously, which strategy is right for you depends a lot on the particulars of your application.
這篇關(guān)于使用實體框架將文件保存在數(shù)據(jù)庫中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!