views:

74

answers:

2

How to get a reference to superior ObjectContext from EntityObject class?

A: 

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.

Nix
A: 

Have a look at the following link:

http://blogs.msdn.com/alexj/archive/2009/06/08/tip-24-how-to-get-the-objectcontext-from-an-entity.aspx

It is like the way that Nix mentioned as an extenxtion to the entity object.

Reza