Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

SaveChangesException when using context.SaveChanges()

Posted on by 145

Hi,

I use early bound LINQ to retrieve a list of incident records on synchronous PostOperation when updating specific field in incident record, i want to change a field called "new_emailApprove" for the first retrieved record , when i use context.SaveChanges() a saveChangesException occurs. I tried to use organizationService.Update() but an exception occurs which says : "EntityState must be set to null, Created (for Create message) or Changed (for Update message)" . Here is my method that implement this:

public static void GetChangeEmailCases(IOrganizationService organizationService ,ITracingService tracingService,Incident preImageCaseEntity)
        {
            svcContext serviceContext = new svcContext(organizationService);
            var cases = (from incident in serviceContext.IncidentSet
                where incident.SubjectId.Id == preImageCaseEntity.SubjectId.Id && incident.CustomerId.Id == preImageCaseEntity.CustomerId.Id && incident.StateCode == 0
                orderby incident.CreatedOn descending 
                select incident).ToList();


            tracingService.Trace("cases length :" + cases.Count);
            if (cases.Any())
            {
                Incident lastCase = cases.First();
                tracingService.Trace(lastCase.CreatedOn.ToString());
                lastCase.new_EmailApprove = true;
                tracingService.Trace(lastCase.Id.ToString());
                serviceContext.UpdateObject(lastCase); // exception here
                
            }
            serviceContext.SaveChanges();
        }


  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: SaveChangesException when using context.SaveChanges()

    Hi Khaled,

    Please have a look below reference -

    community.dynamics.com/.../entitystate-must-be-set-to-null-created-for-create-message-or-changed-for-update-message

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans