Entity Framework 4 选择性延迟加载属性

发布于 2024-09-09 23:39:48 字数 56 浏览 1 评论 0 原文

是否可以加载不包括某些属性的实体?选择该实体的属性之一的成本很高。我想延迟加载这个属性。这可能吗?

Is it possible to load an entity excluding some properties? One of this entity's properties is expensive to select. I would like to lazy load this property. Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乖乖 2024-09-16 23:39:48

既然你看完了大家的回复,我就给你正确的答案。 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

黄昏下泛黄的笔记 2024-09-16 23:39:48

对于标量属性,有选择地不加载某个属性的唯一方法是在 ESQL 或 L2E 中进行投影:

var q = from p in Context.People
        select new
        {
            Id = p.Id,
            Name = p.Name // note no Biography
        };

+1 为 Dan;延迟执行此操作比预先加载更糟糕。如果你想控制加载,就明确一点。

With a scalar property, the only way to selectively not load a certain property is to project in ESQL or L2E:

var q = from p in Context.People
        select new
        {
            Id = p.Id,
            Name = p.Name // note no Biography
        };

+1 to Dan; doing this lazily is worse than loading it up-front. If you want to control loading, be explicit.

樱花细雨 2024-09-16 23:39:48

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

羅雙樹 2024-09-16 23:39:48

假设对 EntityFramework DbSet 进行查询,其中目标实体包含 BigProperty 和 SmallProperty,
您尝试仅访问 SmallProperty 而不将 BigProperty 加载到内存中时:

//this query loads the entire entity returned by FirstOrDefault() in memory
//the execution is deferred during Where; the execution happens at FirstOrDefault
db.BigEntities.Where(filter).FirstOrDefault()?.SmallProperty;

//this query only loads the SmallProperty in memory
//the execution is still deferred during Select; the execution happens at FirstOrDefault
//a subset of properties can be selected from the entity, and only those will be loaded in memory
db.BigEntities.Where(filter).Select(e=>e.SmallProperty).FirstOrDefault();

因此,您可以利用此行为仅查询实际需要的 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 :

//this query loads the entire entity returned by FirstOrDefault() in memory
//the execution is deferred during Where; the execution happens at FirstOrDefault
db.BigEntities.Where(filter).FirstOrDefault()?.SmallProperty;

//this query only loads the SmallProperty in memory
//the execution is still deferred during Select; the execution happens at FirstOrDefault
//a subset of properties can be selected from the entity, and only those will be loaded in memory
db.BigEntities.Where(filter).Select(e=>e.SmallProperty).FirstOrDefault();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文