问题描述
我真的为此感到挠头。我正在尝试使用Dynamics CRM SDK更新帐户记录。无论我尝试什么,都失败了。
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);
给出错误: EntityState必须设置为null,Created(用于Create message)或已更改(用于更新消息)
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
For reference,1. The Account object is created by the SDK Early Bound Code Generation Tool2. crmService is the IOrganizationService connection object3. GetAccounts ... performs a LINQ query and return an IEnumerable
请帮助。
克里斯,
克里斯。
Please help.Thanks,Chris.
推荐答案
请参阅,尤其是多个数据上下文 部分。您似乎在使用多个上下文来跟踪实体。 CrmAccount.GetAccountsBySubmissionCode
方法只会对您隐藏。
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.
请确保在 CrmAccount中.GetAccountsBySubmissionCode
方法来处理上下文/服务,然后返回 IEnumerable< Account>
,或确保对更新
。
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
.
这篇关于Dynamics CRM保存实体更改-获取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!