Search code examples
c#linqdynamics-crm-2011dynamics-crm-2013dynamics-crm-2016

CRM ServiceContext AddObjects not retrievable with LINQ in plugin


I am currently creating some objects, setting their values and then adding them to the context, like this;

var address = new Customaddress();
address.Id = Guid.NewGuid();
address.CustomField = "test";

serviceContext.AddObject(address);

This works fine. However later in the plugin, and before SaveChanges() has been called, I am trying to retrieve a list of these created addresses, like this;

var addresses = serviceContext.CustomAddressSet.Where(...);

The resultant list of addresses contain what was there before my changes. Is there any way to subsequently query a list of added objects from the context before SaveChanges() has been called?

Thanks for any pointers.


Solution

  • No. This doesn't work because you have, presumably, already queried to get values into the context, otherwise just skip the context and create the records using an implementation of IOrganizationService (instead of creating an OrganizationServiceContext which consumed IOrganizationService.)

    And, to be frank, if you are trying to do this you are misuing the OrganizationServiceContext class: the model is CreateContext->Query->Modify->SaveChanges->DisposeContext.