Save files in database with entity framework(使用实体框架将文件保存在数据库中)
问题描述
我有一个基于 Entity Framework 和 Microsoft SQL Server 2008 的 ASP.NET MVC 解决方案.我需要创建一个函数让我的用户上传文件.
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.
我想要的是:
- 使用实体框架将文件存储在数据库中的解决方案
- 一种通过某种哈希/校验和检测并防止两次上传同一文件的解决方案
- 数据库/表设计技巧
推荐答案
对"在 SQL Server 2008 数据库中存储文件的方法是 使用 FILESTREAM 数据类型.我不知道实体框架是否支持这一点,但您当然可以尝试看看会发生什么.
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.
也就是说,大多数时候人们这样做时,他们不会将文件存储在数据库中.这样做意味着您需要通过 ASP.NET 和数据库服务器来提供可以直接从 Web 服务器提供的文件.它还可能使您的数据库和站点的备份图片变得有些复杂.因此,当我们将文件上传到我们的 MVC/Entity Framework 时,我们只存储对数据库中文件位置的引用,并将文件本身存储在其他地方.
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.
这篇关于使用实体框架将文件保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用实体框架将文件保存在数据库中


- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01