如何从Entity对象获取对上级对象的引用 [英] How to get a reference to superior object from Entity object
本文介绍了如何从Entity对象获取对上级对象的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从EntityObject类获取对ObjectContext的引用?
How to get a reference to superior ObjectContext from EntityObject class?
推荐答案
只有通过使用关系的黑客和不分离的实体,才能做到这一点。见下文。
Only way you can do it is via a hack using relationships, and an entity that is not detached. See below.
YourEntity someEntity = null;
RelationshipManager relationshipManager = ((IEntityWithRelationships)someEntity ).RelationshipManager;
IRelatedEnd relatedEnd = relationshipManager.GetAllRelatedEnds().FirstOrDefault();
ObjectQuery getContext = relatedEnd.CreateSourceQuery() as ObjectQuery;
YoutObjectContext c1 = (YourObjectContext)getContext .Context;
祝你好运。如果您使用上面的代码,我建议使用空检查保护它。
Good luck with it. If you use the code above i recommend protecting it with null checks.
这篇关于如何从Entity对象获取对上级对象的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文