Dynamic CRM changes in CRM dynamics - getting errors

I really scratch my head with this. I am trying to use the Dynamics CRM SDK to update an account entry. No matter what I try, this fails. Here it goes.

Account sampleAccount = CrmAccount.GetAccountsBySubmissionCode(crmService, "ERZZUP").Single<Account>(); sampleAccount.Name = "AMC Edited"; crmService.Update(sampleAccount); 

Gives an error: EntityState must be set to null, Created (to create a message) or Modified (for a Refresh message)

 XrmServiceContext ctx = new XrmServiceContext(crmService); Account sampleAccount = CrmAccount.GetAccountsBySubmissionCode(crmService, "ERZZUP").Single<Account>(); sampleAccount.Name = "AMC Edited"; ctx.UpdateObject(sampleAccount); ctx.SaveChanges(); 

Gives an error: the context does not currently track the "account" object.

 XrmServiceContext ctx = new XrmServiceContext(crmService); Account sampleAccount = CrmAccount.GetAccountsBySubmissionCode(crmService, "ERZZUP").Single<Account>(); sampleAccount.Name = "AMC Edited"; ctx.Attach(sampleAccount); ctx.UpdateObject(sampleAccount); ctx.SaveChanges(); 

Gives an error: the "account" object is already bound to the context.

For reference, 1. An Account object is created using the SDK early code generation tool 2. crmService is an IOrganizationService 3 connection object. GetAccounts ... executes a LINQ query and returns IEnumerable

Please, help. Thanks, Chris.

+4
source share
1 answer

Refer to http://msdn.microsoft.com/en-us/library/gg695783.aspx , in particular the "Multiple Data" part. It seems you are using several contexts to track objects. The CrmAccount.GetAccountsBySubmissionCode method just hides this from you.

Make sure that the CrmAccount.GetAccountsBySubmissionCode method removes the context / service before returning an IEnumerable<Account> or make sure you use the same context / service for Update .

+4
source

All Articles