4

我真的为此挠头。我正在尝试使用 Dynamics CRM SDK 来更新客户记录。无论我尝试什么,它都失败了。开始。

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

给出错误:EntityState 必须设置为 null、Created(对于 Create 消息)或 Changed(对于 Update 消息)

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

给出错误:上下文当前未跟踪“帐户”实体。

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();

给出错误:“帐户”实体已附加到上下文。

作为参考, 1. Account 对象由 SDK 早期绑定代码生成工具创建 2. crmService 是 IOrganizationService 连接对象 3. GetAccounts ... 执行 LINQ 查询并返回 IEnumerable

请帮忙。谢谢,克里斯。

4

1 回答 1

4

请参阅http://msdn.microsoft.com/en-us/library/gg695783.aspx,尤其是“多数据上下文”部分。您似乎正在使用多个上下文来跟踪实体。该CrmAccount.GetAccountsBySubmissionCode方法只是对您隐藏了这一点。

确保在CrmAccount.GetAccountsBySubmissionCode返回之前在方法中处理上下文/服务IEnumerable<Account>,或者确保使用相同的上下文/服务到Update.

于 2012-04-18T19:30:29.513 回答