我如何prevent实体框架从加载FILESTREAM字段成字节数组? [英] How do I prevent Entity Framework from loading a FileStream column into a byte array?

查看:157
本文介绍了我如何prevent实体框架从加载FILESTREAM字段成字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个文件存储的应用程序,我们已经将我们数据库中的FileStream类型。该系统预计将支持大型文件。应用程序的一个部分允许多个文档的批量上传。这些文件则必须被链接到系统内的其他实体。

I am developing a file storage application, and we have incorporated the FileStream type in our database. The system is expected to support large files. One portion of the application allows for bulk uploads of multiple documents. These documents then have to be linked to other entities within the system.

一页被设计来显示未链接的文件,以允许用户给一个链接的文件在一个时间实体。做上传过程中的一些负载测试后,我们发现,ASP.NET辅助进程的内存占用加载该文件未链接的页面时飙升至超过GB。

One page is designed to show unlinked documents, to allow a user to link the documents one at a time to entities. After doing some load testing of the upload process, we found that the memory footprint of the ASP.NET worker process spiked to over a GB when loading this Unlinked Documents page.

经过调查,似乎实体框架加载上百未链接文件的整个文​​件的行实体(包括的FileStream,转换为字节数组)。在我的仓库类,转换成模型时,我不救这个字节数组,但那时为时已晚。 EF已经花费的时间和内存的字节数组分配到存储库类重presentation。

After investigation, it seems that Entity Framework is loading the entire document row entity (including the FileStream, converted to a byte array) for hundreds of unlinked documents. In my repository class, I don't save this byte array when converting to a Model, but by then it's too late. EF has spent the time and the memory to allocate the byte array into the Repository class representation.

有没有一种方法,我可以告诉EF不加载此字节数组,除非我明确要求呢?

Is there a way that I can tell EF not to load this byte array unless I explicitly ask for it?

推荐答案

驻军,
你可以使用一个名为实体分裂,你两个相关实体映射到单个表功能。所以在第一个实体,你会拥有所有,但FILESTREAM属性。在第二实体你将有主键(同在第一实体)和文件流属性。创建一个:在模型中的实体之间的一个关系,然后两个实体映射到同一个表。现在,随着FILESTREAM的实体相关的,您可以加载或根据需要延迟加载它。唯一的缺点是,它不是你的主要实体的属性,所以你要浏览到相关实体,然后属性。

Garrison, you can use a feature called "entity splitting" where you map two related entities to a single table. So in the first entity you would have all but the filestream property. In the 2nd entity you would have the primary key (same as in the first entity) and the filestream property. Create a one:one relationship between the entities in your model and then map both entities to the same table. Now the entity with the filestream is related and you can load or lazy load it as needed. The only downside is that it's not a property of your main entity, so you have to navigate to the related entity and then the property.

myEntity.MyRelatedEntity.TheFileStreamProperty

myEntity.MyRelatedEntity.TheFileStreamProperty

下面是一个<一个href=\"https://web.archive.org/web/20130622232956/http://thedatafarm.com/blog/data-access/ef-table-splitting-ndash-the-opposite-of-entity-splitting\"相对=nofollow>旧博客帖子我写了如何做到这一点的EDM设计。

Here's an old blog post I wrote that shows how to do this in the EDM Designer.

您还可以实现与code首先相同的映射。这里有一个MSDN文章我写了一个包含实体分割文章。 http://msdn.microsoft.com/en-us/data/hh272551

You can also achieve the same mapping with Code FIrst. Here's an MSDN article I wrote that contains an entity splitting article. http://msdn.microsoft.com/en-us/data/hh272551

HTH
朱莉

HTH Julie

这篇关于我如何prevent实体框架从加载FILESTREAM字段成字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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