2012-03-01 34 views
6

我生成的實體從CRM這樣的:CRM檢索實體 - 錯誤:無法投類型 'Microsoft.Xrm.Sdk.Entity' 的目的爲類型 'CRMEntities.List'

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'." 

後「加入到觀看」我看到每個組在上下文中的實體有相同的信息。 我錯過了什麼?

回答

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

+0

謝謝!把我的頭撞在牆上,完美地工作! – 2013-02-06 21:43:20

相關問題