使用 Entity Framework 更新 CRM 中的实体记录

Updating an entity record in CRM using Entity Framework

我最近一直在尝试使用 CRM SDK 工具自动生成的代码更新 CRM 2015 实体中的记录 CrmSvcUtil.exe 使用代码

CrmConnection con = new CrmConnection("CRM");
XrmServiceContext ctx = new XrmServiceContext(con);
var nn = ctx.TestEntity.Where(x => x.Name== "12132").FirstOrDefault();
nn.Name="test";
ctx.SaveChanges();

但是保存更改后所有更改都被忽略了,我注意到更改记录的实体状态仍然没有改变。

当使用 ctx.UpdateObject(nn);ctx.Update(mm); 时,应用程序抛出以下错误:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://schemas.microsoft.com/xrm/2011/Contracts/Services:request. The InnerException message was 'Error in line 1 position 13371. Element 'http://schemas.datacontract.org/2004/07/System.Collections.Generic:value' contains data from a type that maps to the name 'http://schemas.microsoft.com/xrm/7.1/Contracts:ConcurrencyBehavior'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'ConcurrencyBehavior' and namespace 'http://schemas.microsoft.com/xrm/7.1/Contracts'.'. Please see InnerException for more details.

或这个

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

并且在尝试手动将实体状态设置为已更改时出现此错误

The entity is read-only and the 'EntityState' property cannot be modified. Use the context to update the entity instead.

知道我可以使用相同的自动生成代码创建新记录 ctx.AddObject()

您应该在调用 ctx.SaveChanges() 之前使用 ctx.UpdateObject

UpdateRequest.ConcurrencyBehavior 是在 CRM 2015 Update 1 中引入的。在我看来,您的 CRM 版本与您使用的 CrmSvcUtil 版本不匹配。您必须使用较新版本的 SDK 工具。

由于您使用的是 CRM 的 RTM 版本,因此您可以下载并使用 CRM 2015 SDK core tools (version 7.0.1) 的相应 RTM 版本。使用该版本的 CrmSvcUtil 重新生成上下文后,您应该不会再看到与 UpdateRequest.ConcurrencyBehavior.

相关的任何错误