从代理延迟加载 POCO 实体
我正在尝试为数据库中已知的实体创建 POCO 代理 - 类似于 nHibernate Session.Load()。
我想设置 ID 值,然后如果在实体上访问任何其他属性,则其他属性将再次延迟加载,类似于 nHibernate Session.Load()
我尝试使用 ObjectContext 创建代理.CreateObject(),设置ID值,并将其附加到上下文。但这些属性在访问时不会延迟加载。然而,导航属性可以延迟加载。
我希望在需要实体并知道 ID,但又不想强制额外的数据库命中来加载实体的情况下使用此功能。我还想确保在需要时可以加载这些属性。
实体框架 4 可以实现这一点吗?
I'm trying to create a POCO proxy for an entity already known to be in the database--similar to the nHibernate Session.Load().
I'd like to set the ID value and then if any other properties are accessed on the entity, the other properties are lazy-loaded--again, similar to the nHibernate Session.Load()
I've tried creating a proxy with ObjectContext.CreateObject(), setting the ID value, and attaching it to the context. But the properties don't lazy-load when accessed. The navigation properties, however, will lazy-load just fine.
I'd like this functionality for cases where I need an entity and know the ID, but don't want to force an extra database hit to load the entity. I also want to ensure that the properties can be loaded in the event that they are needed.
Is this possible with Entity Framework 4?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这是不可能的。原因是实体框架仅支持导航属性的延迟加载。您不能延迟加载标量或复杂属性。您必须从数据库加载实体才能填充它们。
No it is not possible. The reason is that Entity framework supports only lazy loading of navigation properties. You cannot lazy load scalar or complex properties. You must load the entity from database to get them filled.