locked
Context not tracking entity (why in this case?) RRS feed

  • Question

  • Dear all,

    If I execute

                         if (entity.Contains("inf_contactid"))
                        {
                            EntityReference contactId =  (EntityReference)preImage.Attributes["inf_contactid"];
                            Guid mediaSubscriptionRegistrationId = entity.Id;
                            List<inf_mediasubscriptionregistrationdetail> details = (from msrd in pluginManager.OrganizationServiceContext.inf_mediasubscriptionregistrationdetailSet
                                                                                     where msrd.inf_MediaSubscriptionRegistrationId.Id == mediaSubscriptionRegistrationId && msrd.inf_ContactId.Id == contactId.Id
                                                                                     select msrd).ToList();
    
                            foreach (inf_mediasubscriptionregistrationdetail detail in details)
                            {
                                detail.inf_ContactId = (EntityReference)entity.Attributes["inf_contactid"];
    
                                pluginManager.OrganizationServiceContext.UpdateObject(detail);
                            }
                            pluginManager.OrganizationServiceContext.SaveChanges();
                        }

    I get an: "context is not currently tracking" error.

    Ofcourse I can fix it by adding:

                                if (!pluginManager.OrganizationServiceContext.IsAttached(detail))
                                    pluginManager.OrganizationServiceContext.Attach(detail);


    in the loop, but my question is WHY this is happening here?

    I retrieved multiple items through a LINQ query out of the organizationcontext, so I thought all retrieved entities were being tracked automatically. It seems this isn't the case so my understandings of the context is flawed :-)

    Thanks in advance.

    Wednesday, August 21, 2013 7:19 AM

All replies

  • Hi,

    If you have registered a Plug-in on Particular entity which Updates an associated "Detail" entity Here the Plug-in runs under Parent Entity Context.
    When you try to update child record (i.e. Detail entity in your case) it throws out exception, since the context knows nothing about "Detail".  So you have to use the method to track detail entity. 

    if (!pluginManager.OrganizationServiceContext.IsAttached(detail))
                                    pluginManager
    .OrganizationServiceContext.Attach(detail);

    hope this helps!

     

    Wednesday, August 21, 2013 9:18 AM
  • So it doesn't matter if I query the details whatsoever? Queried entities are NOT tracked by default?

    Thanks :)

    Wednesday, August 21, 2013 9:27 AM
  • Hi,

    As per the below blog Entities returned from a LINQ query are automatically attached to the context.

    Refer the below blog for detail explanation.

    http://community.adxstudio.com/blogs/developer/2012-05-18-entity-tracking-in-the-organizationservicecontext/

    Thanks!

    Wednesday, August 21, 2013 10:24 AM
  • Hi,

    As per the below blog Entities returned from a LINQ query are automatically attached to the context.

    Refer the below blog for detail explanation.

    http://community.adxstudio.com/blogs/developer/2012-05-18-entity-tracking-in-the-organizationservicecontext/

    Thanks!

    Thanks for the reply, but if those entities queried are automatically attached to the context, why do I need to attach my "details" again in the example given?

    It doesn't make sense because I queried the "details" from the OrganizationServiceContext.inf_mediasubscriptionregistrationdetailSet..

    Thursday, August 22, 2013 9:52 AM
  • Hi Chiron,

    I was searching for the same answer. As I saw no further discussion on this topic, I thought of sharing my discovery.

    My code is slightly different but you will get the idea.

                var contacts = from cr in svcContext.ContactSet
                                      where cr.contactid.Id == postImage.contactid.Value
                                        && cr.statuscode.Value == (int)contact_statuscode.Draft
                                      select new Contact
                                      {
    //here I am selecting the fields I am interested in, rather than returning all fields.
                                          contactid = cr.contactid,
                                          statuscode = cr.statuscode
                                      }
                                      ;
    
                
                foreach (var cr in contacts)
                {
                    cr.statuscode = new OptionSetValue((int)contact_statuscode.Submitted);
    //here I don't need to Attach as the object is already tracked. Therefore, I just hit UpdateObject after setting the new values as needed.
                    svcContext.UpdateObject(cr);
    
                }
                //this saves only those objects that have been updated.
                svcContext.SaveChanges();

    Hope this helps.

    Thursday, June 11, 2015 7:23 AM