我真的为此感到头疼 . 我正在尝试使用Dynamics CRM SDK更新帐户记录 . 无论我尝试什么,它都失败了 . 开始 .
Account sampleAccount = CrmAccount.GetAccountsBySubmissionCode(crmService, "ERZZUP").Single<Account>();
sampleAccount.Name = "AMC Edited";
crmService.Update(sampleAccount);
给出错误:EntityState必须设置为null,Created(用于创建消息)或Changed(用于Update消息)
XrmServiceContext ctx = new XrmServiceContext(crmService);
Account sampleAccount = CrmAccount.GetAccountsBySubmissionCode(crmService, "ERZZUP").Single<Account>();
sampleAccount.Name = "AMC Edited";
ctx.UpdateObject(sampleAccount);
ctx.SaveChanges();
给出错误:上下文当前未跟踪'account'实体 .
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();
给出错误:'account'实体已附加到上下文 .
供参考,1 . Account对象由SDK Early Bound Code Generation Tool 2创建.crmService是IOrganizationService连接对象3. GetAccounts ...执行LINQ查询并返回IEnumerable
请帮忙 . 谢谢,克里斯 .
1 回答
请参阅http://msdn.microsoft.com/en-us/library/gg695783.aspx,尤其是"Multiple Data Contexts"部分 . 您似乎正在使用多个上下文来跟踪实体 .
CrmAccount.GetAccountsBySubmissionCode
方法只是隐藏了这一点 .确保在
CrmAccount.GetAccountsBySubmissionCode
方法中在返回IEnumerable<Account>
之前处置上下文/服务,或确保使用相同的上下文/服务Update
.