Dynamics CRM saving Entity Changes - Getting Errors

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 07:26:54

问题


I'm really scratching my head with this. I'm trying to use the Dynamics CRM SDK to update an account record. No matter what I try, it's failing. Here goes.

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

Gives the error: EntityState must be set to null, Created (for Create message) or Changed (for Update message)

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

Gives the error: The context is not currently tracking the 'account' entity.

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 the error: The 'account' entity is already attached to a context.

For reference, 1. The Account object is created by the SDK Early Bound Code Generation Tool 2. crmService is the IOrganizationService connection object 3. GetAccounts ... performs a LINQ query and return an IEnumerable

Please help. Thanks, Chris.


回答1:


Refer to http://msdn.microsoft.com/en-us/library/gg695783.aspx, particularly the "Multiple Data Contexts" part. It seems you're using multiple contexts to track the entities. The CrmAccount.GetAccountsBySubmissionCode method just hides this from you.

Make sure within the CrmAccount.GetAccountsBySubmissionCode method to dispose of the context/service before returning the IEnumerable<Account>, or make sure you use the same context/service to Update.



来源:https://stackoverflow.com/questions/10216396/dynamics-crm-saving-entity-changes-getting-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!