【发布时间】:2011-09-08 08:20:13
【问题描述】:
有人可以发布一个确认示例,使用 linq 检索和更新 CRM Dynamics 2011 中的记录。
微软声称这是“不受支持”,但我怀疑这是可能的。
【问题讨论】:
-
您能否参考一下您的断言的来源?
有人可以发布一个确认示例,使用 linq 检索和更新 CRM Dynamics 2011 中的记录。
微软声称这是“不受支持”,但我怀疑这是可能的。
【问题讨论】:
我使用“早期绑定”方法,您可以使用 CrmSvcUtil.exe 工具生成 C# 实体类,但请确保您使用在各种示例中可以找到的 /codecustomization 开关。您需要最新版本的 CRM 2011 SDK,并且必须从其 \bin 文件夹运行 CrmSvcUtil.exe(不要使用随 CRM 安装的版本)。
您的项目需要引用 Microsoft.Xrm.Client、Microsoft.Xrm.Sdk 和 Microsoft.Crm.Sdk.Proxy 以及 .Net 框架中的其他一些内容(查看构建错误以了解您缺少什么,然后添加它们直到构建完成) .
这是一个基本代码 sn-p,它检索联系人实体,更新其中一个字段,然后将其保存回 CRM:
CrmDataContext dc = new CrmDataContext("Xrm");
Contact contact = (from c in dc.ContactSet
where ...whatever...
select c).FirstOrDefault();
contact.FirstName = "Jo";
dc.SaveChanges();
(注意CrmDataContext 是我的数据上下文的名称。您可以使用CrmSvcUtil 命令行开关之一设置此名称。
您还需要在 web.config 中添加一些内容:
<configSections>
<section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client" />
</configSections>
<connectionStrings>
<add name="Xrm" connectionString="Server=http://<your crm url>; Domain=<your domain>; Username=<a crm user id>; Password=<their password>" />
</connectionStrings>
<microsoft.xrm.client>
<contexts>
<add name="Xrm" type="" />
</contexts>
</microsoft.xrm.client>
这是假设您在公司网络上运行 CRM,因此连接字符串中指定的帐户和域将是 AD 帐户,该帐户被设置为具有检索和更新实体的相关权限的 CRM 用户。
【讨论】:
这是使用连接到在线提供商的 ODATA 提供商的粗略示例
var serverConfig = GetServerConfig(sessionKey);
// Connect to the Organization service.
// The using statement ensures that the service proxy will be properly disposed.
using (var serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials))
{
// This statement is required to enable early-bound type support.
serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
using (var orgContext = new CrmServiceContext(serviceProxy))
{
return orgContext.AccountSet.Where(item => item.Id == id).Select().Single();
}
}
SDK 中还有一个很好的例子:
CRM2011Sdk\sdk\samplecode\cs\wsdlbasedproxies\online
【讨论】: