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

laz*_*rus 6 c# early-binding dynamics-crm-2011

我从CRM生成实体,如下所示:

CrmSvcUtil.exe /url:http://<servername>/<organizationname>/XRMServices/2011/Organization.svc
    /out:<outputfilename>.cs /username:<username> /password:<password> /domain:<domainname>
    /namespace:CRMEntities /serviceContextName:XrmServiceContext
Run Code Online (Sandbox Code Playgroud)

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

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

我收到错误:

Message "Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'CRMEntities.List'."
Run Code Online (Sandbox Code Playgroud)

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

laz*_*rus 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();
Run Code Online (Sandbox Code Playgroud)

关键是:orgserv.EnableProxyTypes();