CRM检索实体 - 错误:无法将“Microsoft.Xrm.Sdk.Entity”类型的对象强制转换为“CRMEntities.List”

时间:2012-03-01 15:32:13

标签: c# dynamics-crm-2011 early-binding

我从CRM生成实体:

CrmSvcUtil.exe /url:http://<servername>/<organizationname>/XRMServices/2011/Organization.svc
    /out:<outputfilename>.cs /username:<username> /password:<password> /domain:<domainname>
    /namespace:CRMEntities /serviceContextName:XrmServiceContext

对于 serviceContextName ,我设置 XrmServiceContext 。 我想使用下一个代码从CRM中检索一些实体:

var context = new XrmServiceContext(myorgserv);
var marketingList = context.ListSet.Where(item => item.Id == new Guid("SOME GUID"));

我收到错误:

Message "Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'CRMEntities.List'."

在'添加到监视'之后,我看到上下文中的每组实体都有相同的消息。 我错过了什么?

1 个答案:

答案 0 :(得分:16)

问题解决了。初始化 OrganizationServiceProxy 后,我必须调用 EnableProxyTypes 方法。

OrganizationServiceProxy orgserv;
ClientCredentials clientCreds = new ClientCredentials();

    clientCreds.Windows.ClientCredential.UserName = username;
    clientCreds.Windows.ClientCredential.Password = password;
    clientCreds.Windows.ClientCredential.Domain = domain;
    IServiceConfiguration<IOrganizationService> orgConfigInfo = ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(orgServiceUri);

    orgserv = new OrganizationServiceProxy(orgConfigInfo, clientCreds);
    orgserv.EnableProxyTypes();

关键是: orgserv.EnableProxyTypes();