EDM: How to obtain the ObjectContext from an EntityObject

  • Thread starter Thread starter markla
  • Start date Start date
M

markla

Hi,

Is there a way to find the ObjectContext to which an EntityObject is
attached, from an EntityObject-derived reference alone?

Reason: so I don't need to pass the ObjectContext around- if I need the
ObjectContext (eg. to delete an entity), I want to infer it from the
EntityObject-derived Entity reference.

Thanks,
+Mark
 
Hello Mark,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you. Based on my
understanding, the objective is trying to get the ObjectContext from an
instance of EntityObject. If I have misunderstood the issue, please feel
free to let me know.

This issue is discussed in the LINQ to Entity MSDN forum before in this
link
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3801783&SiteID=1. As
the product team member Jaroslaw indicates, it is not directly possible to
get from the entity object to the context. But there are some workarounds
you may want to consider. I just repost them here,

1. Move the method to a service class (CouponRetrievalService, etc) (why is
it declared on the entity type itself if it performs operations on
completely unrelated objects?) and pass ObjectContext in the constructor of
the service. This is the recommended approach.

2. Pass the context as a parameter to the Foo() method.

3. Have some form of an ambient context (global or thread-local static
variable)

4. If your entity has at least one relationship, you can get to the context
using the following trick:

((ObjectQuery)((IEntityWithRelationships)this).RelationshipManager.GetAllRel
atedEnds().First().CreateSourceQuery()).Context
but this method is not very fast and in general not recommended because of
its constraints.

5. As a last resort, you can use reflection to get the context from some
internal data structures, such as RelationshipManager, but that is
completely unsupported.

The recommended way is as you have already found, passing the ObjectContext
as parameter since this way is easy to implement. Please let me know if
this information helps for your scenario or not. If you have any other
questions or concerns, please feel free to let me know. I will try my best
to provide future help.


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi,

Thanks for your prompt reply, appreciated. I'll take your advice and include
the context as a parameter.

Cheers,
+Mark

""Ji Zhou [MSFT]"" said:
Hello Mark,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you. Based on my
understanding, the objective is trying to get the ObjectContext from an
instance of EntityObject. If I have misunderstood the issue, please feel
free to let me know.

This issue is discussed in the LINQ to Entity MSDN forum before in this
link
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3801783&SiteID=1. As
the product team member Jaroslaw indicates, it is not directly possible to
get from the entity object to the context. But there are some workarounds
you may want to consider. I just repost them here,

1. Move the method to a service class (CouponRetrievalService, etc) (why
is
it declared on the entity type itself if it performs operations on
completely unrelated objects?) and pass ObjectContext in the constructor
of
the service. This is the recommended approach.

2. Pass the context as a parameter to the Foo() method.

3. Have some form of an ambient context (global or thread-local static
variable)

4. If your entity has at least one relationship, you can get to the
context
using the following trick:

((ObjectQuery)((IEntityWithRelationships)this).RelationshipManager.GetAllRel
atedEnds().First().CreateSourceQuery()).Context
but this method is not very fast and in general not recommended because of
its constraints.

5. As a last resort, you can use reflection to get the context from some
internal data structures, such as RelationshipManager, but that is
completely unsupported.

The recommended way is as you have already found, passing the
ObjectContext
as parameter since this way is easy to implement. Please let me know if
this information helps for your scenario or not. If you have any other
questions or concerns, please feel free to let me know. I will try my best
to provide future help.


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
There is an additional trick that might work, I did it for LINQ to SQL
and if you can find out the class structure for EF you might be able to
re-write it to fit.

Have a look here:
http://www.lowendahl.net/showShout.aspx?id=184
http://www.lowendahl.net/showShout.aspx?id=185


--
Patrik Löwendahl
http://www.lowendahl.net
[MVP] [MCT - Enterprise Application Development]

(e-mail address removed) skrev:
Hello Mark,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you. Based on my
understanding, the objective is trying to get the ObjectContext from an
instance of EntityObject. If I have misunderstood the issue, please feel
free to let me know.

This issue is discussed in the LINQ to Entity MSDN forum before in this
link
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3801783&SiteID=1. As
the product team member Jaroslaw indicates, it is not directly possible to
get from the entity object to the context. But there are some workarounds
you may want to consider. I just repost them here,

1. Move the method to a service class (CouponRetrievalService, etc) (why is
it declared on the entity type itself if it performs operations on
completely unrelated objects?) and pass ObjectContext in the constructor of
the service. This is the recommended approach.

2. Pass the context as a parameter to the Foo() method.

3. Have some form of an ambient context (global or thread-local static
variable)

4. If your entity has at least one relationship, you can get to the context
using the following trick:

((ObjectQuery)((IEntityWithRelationships)this).RelationshipManager.GetAllRel
atedEnds().First().CreateSourceQuery()).Context
but this method is not very fast and in general not recommended because of
its constraints.

5. As a last resort, you can use reflection to get the context from some
internal data structures, such as RelationshipManager, but that is
completely unsupported.

The recommended way is as you have already found, passing the ObjectContext
as parameter since this way is easy to implement. Please let me know if
this information helps for your scenario or not. If you have any other
questions or concerns, please feel free to let me know. I will try my best
to provide future help.


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top