C# System.InvalidCastException:Microsoft Dynamics CRM遇到错误
我正在尝试使用Microsoft Dynamics CRM SDK以编程方式更新记录。不幸的是,在我提取记录、更新字段并在我的服务代理上调用C# System.InvalidCastException:Microsoft Dynamics CRM遇到错误,c#,dynamics-crm-2011,dynamics-crm,dynamics-crm-4,dynamics-crm-2013,C#,Dynamics Crm 2011,Dynamics Crm,Dynamics Crm 4,Dynamics Crm 2013,我正在尝试使用Microsoft Dynamics CRM SDK以编程方式更新记录。不幸的是,在我提取记录、更新字段并在我的服务代理上调用.update()方法后,我得到以下异常: [System.ServiceModel.FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>] System.InvalidCastException: Microsoft Dynamics CRM has experienced an
.update()
方法后,我得到以下异常:
[System.ServiceModel.FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>]
System.InvalidCastException: Microsoft Dynamics CRM has experienced an error.
Reference number for administrators or support: #B08B34D9"
System.ServiceModel.FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>
下面是我用来创建传递给Dynamics的实体
对象的方法。数据从实体框架模型对象中提取并映射到适当的字段
private Entity CreateAccount(AgentTransmission agt, Guid Id = new Guid())
{
_account = new Entity();
_account.LogicalName = "account";
_account.Attributes.Add("name", agt.AgencyName);
_account.Attributes.Add("telephone1", agt.BusinessPhone.Replace("(","").Replace(")", "").Replace("-", ""));
_account.Attributes.Add("address1_line1", agt.MailingStreet1);
_account.Attributes.Add("address1_city", agt.MailingCity);
_account.Attributes.Add("address1_postalcode", agt.MailingZip);
_account.Attributes.Add("neu_address1stateprovince", LookupStateCode(agt.MailingState));
_account.Attributes.Add("address1_addresstypecode", new OptionSetValue(1)); //1 for Mailing
_account.Attributes.Add("neu_channelid", LookupChannelId(agt.Channel));
_account.Attributes.Add("neu_appointmentstatus", new OptionSetValue(279660000));
_account.Attributes.Add("customertypecode", LookupCustomerCode(agt.RelationshipType));
_account.Attributes.Add("neu_taxid", UnobfuscateRef(agt.ReferenceNumber).ToString());
//If we're doing an update will need the GUID. Only use when Update is needed.
if (Id != Guid.Empty)
{
_account.Attributes.Add("accountid", Id);
}
return _account;
}
然而,
.Create()
方法按预期工作,因此我认为异常被隔离到用于更新的accountid
GUID属性 在处理现有实体时,在CreateAccount
方法中,尝试更改此行:
_account.Attributes.Add("accountid", Id);
到
从理论上讲,这并不重要,但是应该始终为现有记录设置实体实例的Id
属性
您可能还想考虑传递一个帐户实体(您称之为“for for循环”中的联系人),而不是将ID作为代码输入到<代码> CreateAccount <代码>中,这样您就不必在每个调用中显式创建一个帐户实例,也可以删除试图分配ID的逻辑。(因为传入实体已经定义了它)
例如,您的电话可能是:_servicePoxy.Create(CreateAccount(agt), new Entity("account"));
及
然后您可以从CreateAccount
中删除这些行:
_account = new Entity();
_account.LogicalName = "account";
及
(我本想把这篇文章更多地作为评论发表,但我没有足够的权限。)System.ServiceModel.FaultException会吃掉堆栈跟踪。引用作为一种方法来实际记录堆栈跟踪,以便能够找到错误发生的位置。需要注意的一点是,您显然是从其他地方复制了一些代码,因为您检索了帐户记录,但foreach调用了它们“联系人”
foreach(collection.Entities中的实体联系人)
。虽然这会起作用,但可能会提示您复制了其他部分,从而导致您看到的问题。我怀疑您的Lookup*helper函数之一返回了不匹配的数据类型。InvalidCastException是一个大提示,一个或多个属性设置为错误的类型,而这可能是一个很好的提示…没有上下文作为to如果设置的属性不正确,那么这实际上是一条无用的(非常令人沮丧的)消息。
_servicePoxy.Create(CreateAccount(agt), new Entity("account"));
_servicePoxy.Update(CreateAccount(agt, contact));
_account = new Entity();
_account.LogicalName = "account";
if (Id != Guid.Empty)
{
_account.Attributes.Add("accountid", Id);
}