开发者

why can't we get ObjectContext from an EntityObject

It is well know that if we have an EntityObject that there is no way to find the ObjectContext that it belongs to. That's fair enough I guess, but why is it that we can lazy load objects then? Surely the process of lazy loading has to get access to the ObjectContext in order to load t开发者_Go百科he new objects?


The accepted answer is limited in that it can only work if the entity has at least one relationship.

However, this can also be done via reflection:

public ObjectContext Context(EntityObject entity) {
    var relationshipManager = ((IEntityWithRelationships)entity).RelationshipManager;
    var wrappedOwnerProperty = relationshipManager.GetType().GetProperty("WrappedOwner",BindingFlags.Instance | BindingFlags.NonPublic);
    var wrappedOwner = wrappedOwnerProperty.GetValue(relationshipManager);
    var contextProperty = wrappedOwner.GetType().GetProperty("Context");
    return (ObjectContext)contextProperty.GetValue(wrappedOwner);
}

In VB.NET:

Function Context(entity As EntityObject) As ObjectContext
    Dim relationshipManager = DirectCast(entity, IEntityWithRelationships).RelationshipManager
    Dim wrappedOwnerProperty = relationshipManager.GetType.GetProperty("WrappedOwner", BindingFlags.Instance Or BindingFlags.NonPublic)
    Return wrappedOwnerProperty.GetValue(relationshipManager).Context
End Function

NB: This was tested under .NET Framework v. 4.5.1. YMMV, as this depends on the internal WrappedOwner property and the Context property on the internal BaseEntityWrapper<TEntity> class. Nevertheless, if earlier/leter versions of .NET have different internal properties/classes it should be simple enough to do something similar.

NB: This could further be improved by making it an extension method on EntityObject, and by taking a generic parameter to return a strongly typed ObjectContext. It could also be simplified by using some sort of method to get property values by name.


You are right, given an object, we don't know what context it belongs to, or what session it is attached to. But Lazy Loading happens like this :

var firstPost = _Context.Posts.First()
var commentList = firstPost.Comments

When you say _Context.Posts.First() then one post is loaded. Then when you say firstPost.Comments that's when the comments list is loaded.

This is possible because the type of your Comments field in your Post is probably an IList or some such generic interface : this is because EF4 can put a proxy list instead of the actual comment list. The proxy list knows about the _Context - knows about which session or context it is attached to. Hence it is able to load an actual list on demand.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜