如何防止Entity Framework将FileStream列加载到字节数组? [英] How do I prevent Entity Framework from loading a FileStream column into a byte array?

查看:96
本文介绍了如何防止Entity Framework将FileStream列加载到字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



一页旨在显示未链接的文档,以允许用户一次一个地链接文档到实体。在对上传过程进行了一些负载测试之后,我们发现ASP.NET工作进程的内存占用在加载Unlinked Documents页面时超过了GB。



经过调查,Entity Framework似乎正在将整个文档行实体(包括FileStream,转换为字节数组)加载到数百个未链接的文档中。在我的存储库类中,当转换为模型时,我不保存这个字节数组,但是到现在为时已晚。 EF已经花费了时间和内存将字节数组分配到Repository类表示中。



有没有办法可以告诉EF不加载这个字节数组,除非我明确要求吗?

解决方案

Garrison,
您可以使用称为实体拆分的功能,将两个相关实体映射到单个表。所以在第一个实体中,你将只有filestream属性。在第二个实体中,您将拥有主键(与第一个实体相同)和filestream属性。在模型中创建一个实体之间的一个关系,然后将两个实体映射到同一个表。现在,具有filestream的实体是相关的,您可以根据需要加载或延迟加载它。唯一的缺点是,它不是您的主要实体的财产,因此您必须导航到相关实体,然后导航到该属性。



myEntity.MyRelatedEntity.FileStreamProperty



这是一个旧博客帖子我写道,它显示了如何在EDM Designer中执行此操作。



您还可以使用Code FIrst实现相同的映射。这是一篇我写的包含一个实体分割文章的MSDN文章。 http://msdn.microsoft.com/en-us/data/hh272551



HTH
朱莉


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.

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.

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.

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

解决方案

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

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

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 Julie

这篇关于如何防止Entity Framework将FileStream列加载到字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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