Thursday 22 March 2012

The context is not currently tracking the entity

I encountered this error when I deploy a plug-in to CRM online environment. I tested the same plug-in in on premises environment and it works without any error.

The plugin read and write to entity A and entity B (they have no relationships). The plug-in is triggered when entity A records are created and it retrieve all records from entity B and process them.

The error was thrown when it tries to save changes to entity B, with this error message: The context is not currently tracking the <<entity B>> entity

It suggested the services context does not track <<entity B>> in the scope. I researched this topic and found out I need to use OrganizationServiceContext.Attach Method. However when I use this method it returns with this error: The context is already tracking the <<entity B>> entity. :(

And did more research and found that it actually need to detach the object ( OrganizationServiceContext.Detach Method) before it can be attached.

This makes no sense to me but this is the work around.

No comments:

Post a Comment