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