CRM Retrieve entity - Error: Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'CRMEntities.List'

拥有回忆 提交于 2019-12-06 01:40:06

问题


I generated entities from CRM like this:

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

For serviceContextName I set XrmServiceContext. I want to retrieve some entity from CRM using next code:

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

And I'm getting error:

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

After 'add to watch' I saw that every set of entities in context have the same message. What have I missed?


回答1:


Problem solved. After initializing OrganizationServiceProxy, I have to call EnableProxyTypes method.

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();

Key thing is: orgserv.EnableProxyTypes();



来源:https://stackoverflow.com/questions/9518994/crm-retrieve-entity-error-unable-to-cast-object-of-type-microsoft-xrm-sdk-en

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!