使用实体框架将文件保存在数据库中 [英] Save files in database with entity framework

查看:24
本文介绍了使用实体框架将文件保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 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.

这篇关于使用实体框架将文件保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆