Entity Framework 4 选择性延迟加载属性
是否可以加载不包括某些属性的实体?选择该实体的属性之一的成本很高。我想延迟加载这个属性。这可能吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否可以加载不包括某些属性的实体?选择该实体的属性之一的成本很高。我想延迟加载这个属性。这可能吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
既然你看完了大家的回复,我就给你正确的答案。 EF 不支持延迟加载属性。然而它确实支持了一个比这个更强大的概念。这称为表拆分,您可以将表映射到两个实体。假设数据库中的产品表可以映射到产品实体和ProductDetail实体。然后,您可以将昂贵的字段移至 ProductDetail 实体,然后在 prodcut 和 Productdetail 实体之间创建 1..1 关联。然后,您可以仅在需要时延迟加载产品详细信息关联。
在我的书的表演章节中,我有一个名为的食谱。
13-9。将昂贵的房产转移到另一个实体
希望有帮助!
Julie Lerman 有一篇关于如何拆分表的文章
Now that you have read everyone's reply, I will give you the correct answer. EF does not support lazy loading of properties. However it does support a much powerful concept then this. It's called table splitting where you can map a table to two entities. Say a product table in the the database can be mapped to product entity and ProductDetail entity. You can then move the expensive fields to the ProductDetail entity and then create a 1..1 association between prodcut and productdetail entity. You can then lazy load the productdetail association only when you need it.
In my performance chapter of my book, I have a recipe called.
13-9. Moving an Expensive Property to Another Entity
Hope that helps!
Julie Lerman has an article on how to split a table
对于标量属性,有选择地不加载某个属性的唯一方法是在 ESQL 或 L2E 中进行投影:
+1 为 Dan;延迟执行此操作比预先加载更糟糕。如果你想控制加载,就明确一点。
With a scalar property, the only way to selectively not load a certain property is to project in ESQL or L2E:
+1 to Dan; doing this lazily is worse than loading it up-front. If you want to control loading, be explicit.
stimms 是正确的,但使用延迟加载时要小心。您可能会遇到性能问题,并且没有意识到该属性正在代码中的特定位置加载。这是因为它在使用属性时加载数据
我更喜欢使用显式加载。这样您就知道它们何时加载以及在哪里加载。以下链接提供了 LoadProperty http://sankarsan.wordpress.com/2010/05/09/ado-net-entity-framework-data-loading-part-2/
您还可以使用热切加载包括方法。示例如下:http://wildermuth.com/2008/12/28/Caution_when_Eager_Loading_in_the_Entity_Framework
stimms is correct, but be careful while using lazy loading. You may have performance issues and not realize the property is getting loaded at a specific location in your code. This is because it loads the data when you use the property
I prefer to use explicit loading. This way you know when they get loaded and where. Here's a link that gives an example for the LoadProperty http://sankarsan.wordpress.com/2010/05/09/ado-net-entity-framework-data-loading-part-2/
You can also you Eager Loading by using the Include method. Example here:http://wildermuth.com/2008/12/28/Caution_when_Eager_Loading_in_the_Entity_Framework
假设对 EntityFramework DbSet 进行查询,其中目标实体包含 BigProperty 和 SmallProperty,
当您尝试仅访问 SmallProperty 而不将 BigProperty 加载到内存中时:
因此,您可以利用此行为仅查询实际需要的 BigProperty,并使用 select 语句显式地将其过滤掉其他地方。
我使用 Visual Studio 调试诊断工具的内存使用功能对此进行了测试。
Given a query over an EntityFramework DbSet, where the targeted entity contains a BigProperty and a SmallProperty,
When you're trying to only access the SmallProperty without loading the BigProperty in memory :
Therefore you could exploit this behaviour to only query the BigProperty where you actually need it, and use select statements to explicitly filter it out everywhere else.
I tested this with the Memory Usage functionality from the Visual Studio debug Diagnostic Tools.