Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum
Answered

The context is not currently tracking the entity

Posted on by 145

Hi,

I'm using LINQ to get list of case records using the service context from the generated XRM , when i try to resolve the first case in the list it works fine, but when i try to resolve a different case in the same list it doesn't work , an exception occurs : "The context is not currently tracking the 'incident' entity" . What might be the problem ? it feels like this is  a non sense issue.

This works :

svcContext serviceContext = new svcContext(organizationService);
Incident preImageCaseEntity = context.PreEntityImages["PreBusinessEntity"].ToEntity<Incident>();
List<Incident> cases = GetChangeEmailCases(organizationService, preImageCaseEntity, serviceContext);
if (cases.Any())
{
tracingService.Trace(cases.Count.ToString());
Incident lastChangeEmailCase = cases.First();
serviceContext.UpdateObject(lastChangeEmailCase);
serviceContext.SaveChanges();

}

but this doesn't work:

svcContext serviceContext = new svcContext(organizationService);
Incident preImageCaseEntity = context.PreEntityImages["PreBusinessEntity"].ToEntity<Incident>();
List<Incident> cases = GetChangeEmailCases(organizationService, preImageCaseEntity, serviceContext);
if (cases.Any())
{
tracingService.Trace(cases.Count.ToString());
Incident lastChangeEmailCase = cases[1];
serviceContext.UpdateObject(lastChangeEmailCase);
serviceContext.SaveChanges();

}

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: The context is not currently tracking the entity

    Hi Sara,

    I am also facing similar kind of issue and trying to use the code that you have suggested but the serviceContext is not having the IsAttached() method available.

    Kindly help me with this

    Thanks,

    Kiran

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: The context is not currently tracking the entity

    Hi Khaled, 

    A cleaner way would be to have a function to save the changes and call them. 

    public void SaveChanges(OrganizationServiceContext serviceContext, Incident incidentToUpdate)
    {
        if (incidentToUpdate.recordId != Guid.Empty)
        {
            if (!serviceContext.IsAttached(incidentToUpdate))
            {
                serviceContext.Attach(incidentToUpdate);
            }
    
            serviceContext.UpdateObject(incidentToUpdate);
            serviceContext.SaveChanges();
        }
    }
    


    Essentially, you are checking the serviceContext has the incident record or not...if not, attach it and then update the record; else just update the record. 

    - Sara

    Code typed here without compiler verification / testing. Please modify typos accordingly. 

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: The context is not currently tracking the entity

    You need to make sure that you attach the incident record to the serviceContext.

    Code snippet as below. 

    if(!serviceContext.IsAttached(lastChangeEmailCase))
    {
       serviceContext.Attach(lastChangeEmailCase);
    serviceContext.UpdateObject(lastChangeEmailCase);
    serviceContext.SaveChanges(); }


    - Sara

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,406 on at
    RE: The context is not currently tracking the entity

    Hi Khaled,

    Remove the save changes statement before the second case and try again.

    ===========

    Incident case = cases[0];

    case.new_emailapprove = true;

    serviceContext.UpdateObject(case);

    serviceContext.SaveChanges(); // Remove this line and try again.

    Incident case = cases[1];

    case.new_emailapprove = true;

    serviceContext.UpdateObject(case);

    serviceContext.SaveChanges();

    ===========

    Hope this helps.

  • Khaled Rimawi Profile Picture
    Khaled Rimawi 145 on at
    RE: The context is not currently tracking the entity

    Hi Sara,

    Yes, i'm pretty sure.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: The context is not currently tracking the entity

    Are you sure the following line actually returns an incident record? It could very well be a case of the list containing only one record. 

    Incident lastChangeEmailCase = cases[1];
    


  • Suggested answer
    a33ik Profile Picture
    a33ik 84,313 Super User on at
    RE: The context is not currently tracking the entity

    Please, don't create duplicated threads.

  • Khaled Rimawi Profile Picture
    Khaled Rimawi 145 on at
    Updating list of entities not working - Plugins

    Hi, i am trying to update list of case entities, but an exception occurs : "the context is not currently tracking 'incident' entity" . if i only update one entity it works fine , but when i update multiple entities the exception happens.

    //This Works fine
    svcContext serviceContext = new svcContext(organizationService);
    Incident preImageCaseEntity = context.PreEntityImages["PreBusinessEntity"].ToEntity<Incident>();
    List<Incident> cases = GetChangeEmailCases(organizationService, preImageCaseEntity, serviceContext);
    
    Incident case = cases[0];
    case.new_emailapprove = true;
    serviceContext.UpdateObject(case);
    serviceContext.SaveChanges();
    
    //--------------------------------
    //this is not working
    svcContext serviceContext = new svcContext(organizationService);
    Incident preImageCaseEntity = context.PreEntityImages["PreBusinessEntity"].ToEntity<Incident>();
    List<Incident> cases = GetChangeEmailCases(organizationService, preImageCaseEntity, serviceContext);
    
    Incident case = cases[0];
    case.new_emailapprove = true;
    serviceContext.UpdateObject(case);
    serviceContext.SaveChanges();
    Incident case = cases[1];
    case.new_emailapprove = true;
    serviceContext.UpdateObject(case);
    serviceContext.SaveChanges();


Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,995 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,610 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans