字段'Target'缺少必需成员'LogicalName'

时间:2014-10-02 22:05:19

标签: c# odata wcf-data-services xrm

我使用Microsoft XRM SDK以编程方式添加实体。但是,每次运行.Create()命令时,都会出现以下错误:

Required member 'LogicalName' missing for field 'Target'

首次在我们公司使用此服务和类似资源很少,所以不确定此错误的含义或如何调查/解决它。

以下是我为处理XRM通信而创建的类。我实例化construtor中的每个连接属性。然后,在这种情况下,请致电CreateAgency(AgentTransmission agt)CreateAgency()方法调用的.Create(account)方法中抛出了异常。

class DynamicsCommunication
{
    private Uri OrganizationUri = new Uri("http://devhildy03/xRMDRMu01/XRMServices/2011/Organization.svc");
    private ClientCredentials credentials;
    private OrganizationServiceProxy servicePoxy;
    private Guid accountId;
    private Entity account;

    public DynamicsCommunication()
    {
        credentials = new ClientCredentials();
        credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
        servicePoxy = new OrganizationServiceProxy(OrganizationUri, null, credentials, null);
        accountId = Guid.Empty;
    }

    public string UpdateDynamics(AgentTransmission agt)
    {
        switch (DeterminAction(agt))
        {
            case DynamicsAction.Create:
                return CreateAgency(agt);
            case DynamicsAction.Update:
                return UpdateAgency(agt);
            default:
                return string.Empty;
        }
    }

    private string CreateAgency(AgentTransmission agt)
    {
        try
        {
            //Exception is thrown after this command
            accountId = servicePoxy.Create(CreateAccount(agt));

            if (accountId != Guid.Empty)
            {
                return string.Empty;
            }
            else
            {
                return "error creating agency";
            }
        }
        catch (ODataException oEx)
        {
            string s = oEx.Message;
            throw;
        }
        catch (Exception ex)
        {
            string s = ex.Message;
            throw;
        }
    }

    private Entity CreateAccount(AgentTransmission agt)
    {
        account = new Entity();
        account.Attributes.Add("LogicalName", "something");
        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", 1); //1 for Mailing
        account.Attributes.Add("neu_channelid", LookupChannelId(agt.Channel));
        account.Attributes.Add("neu_appointementstatus", "279660000");
        account.Attributes.Add("customertypecode", LookupCustomerCode(agt.RelationshipType));
        account.Attributes.Add("neu_taxid", UnobfuscateRef(agt.ReferenceNumber));

        return account;
    }
}

1 个答案:

答案 0 :(得分:5)

在Entity对象的LogicalName属性上设置CRM实体的名称,而不是将其添加到属性集合

account = new Entity("your_entity_name");

account = new Entity();
account.LogicalName = "your_entity_name";