EF4 测试集合或相关实体是否是自跟踪实体的代理
总之,
我从 Entity Framework 4(还不是 CTP5)开始,正在研究将 NHibernate 迁移到 EF 的复杂性。
我们有一些代码测试集合或相关实体是否是代理,如果是,则执行一些与正常情况不同的代码。如果集合不是代理(即它已经加载到上下文中),那么我们直接针对集合执行其他操作。
我已将此代码迁移为对集合使用 IRelatedEnd 接口,但将 ICollection 转换为 IRelatedEnd 的行为会导致 EF 加载集合...结果是,当我检查 IsLoaded == true 时,集合永远不是代理在下一行。
考虑到这是自定义 POCO 还是这是预期行为,我是否需要做一些额外的事情?
同样,是否有办法测试相关实体是否是代理?
非常感谢
All,
I'm starting out with Entity Framework 4 (not CTP5 yet) and am looking at the complexities of migrating NHibernate to EF.
Some code we have tests if a collection or related entity is a proxy and, if so, performs some different code to normal. If the collection is not a proxy (i.e. it has already been loaded into the context) then we do something else directly against the collection.
I've migrated this code to use the IRelatedEnd interface for collections, but the act of casting my ICollection to IRelatedEnd causes EF to load the collection... the result is that the collection is never a proxy when I check for IsLoaded == true in the next line.
Do I need to do something extra considering this is a custom POCO or is this expected behaviour?
Likewise, is there anyway to test if a related entity is a proxy or not?
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MSDN 上建议的测试代理的方法是使用类似的方法this:
此方法适用于 EF 4 和 EF 4.1 Codefirst。
至于延迟加载,我从未使用过 IRelatedEnd - 只是将 POCO 中的导航属性保留为 ICollection,并确保启用延迟加载。除此之外,它就可以了。
edmx 中还有一个用于延迟加载的选项。
The suggested method over on MSDN to test for proxies is to use something like this:
This method works in both EF 4 and EF 4.1 Codefirst.
As for the lazy loading, I've never use the IRelatedEnd - just left the navigation properties in the POCO's as ICollection and ensure that lazy loading is enabled. Beyond that, it just works.
There's also an option in the edmx for lazy loading.