Dynamics crm 2011 微软动态CRM 2011。通过ID获取实体的通用方法

Dynamics crm 2011 微软动态CRM 2011。通过ID获取实体的通用方法,dynamics-crm-2011,dynamics-crm,Dynamics Crm 2011,Dynamics Crm,在CRM 4.0中,我的存储库中有一个通用方法: 公共T GetEntityById(Guid id) { var实体= 来自m_context.GetEntities中的e(typeof(T).Name) 其中e.GetPropertyValue(IdentityFieldName)==id 选择e; 返回(T)个实体。FirstOrDefault(); } 但是CRM 2011呢ICmentity 通过ID获取通用实体的替代方法是什么 类似于(T)m_context.Retrieve(ty

在CRM 4.0中,我的存储库中有一个通用方法:

公共T GetEntityById(Guid id) { var实体= 来自m_context.GetEntities中的e(typeof(T).Name) 其中e.GetPropertyValue(IdentityFieldName)==id 选择e; 返回(T)个实体。FirstOrDefault(); } 但是CRM 2011呢<缺少带有GetPropertyValue方法的代码>ICmentity

通过ID获取通用实体的替代方法是什么

类似于
(T)m_context.Retrieve(typeof(T).Name,id,new ColumnSet())


参见

在寻找相同答案时发现了这个问题,这就是我最终解决它的原因

    public T GetEntityByID<T>(Guid guid) where T : Entity
    {
        return (T) (_organizationService.Retrieve((typeof(T)).Name, guid, new ColumnSet()));
    }
public T GetEntityByID(Guid),其中T:Entity
{
return(T)(_organizationService.Retrieve((typeof(T)).Name,guid,new ColumnSet());
}

您确实希望使用ToEntity方法,而不是强制转换。有时(T).Name的类型会有大小写差异,因此我还编写了一个helper函数:

//
///检索具有给定Id和给定列的给定类型的实体
/// 
///早期绑定的实体类型
///开放式组织服务
///实体主键
///要检索的列
/// 
公共静态T GetEntity(此IOrOrganizationService服务,Guid id,ColumnSet ColumnSet)
其中T:实体
{
return service.Retrieve(EntityHelper.GetEntityLogicalName(),id,columnSet).ToEntity()
}
公共静态字符串GetEntityLogicalName(),其中T:Entity
{
返回GetEntityLogicalName(typeof(T));
}
公共静态字符串GetEntityLogicalName(类型)
{
var field=type.GetField(“EntityLogicalName”);
如果(字段==null)
{
如果(类型==类型(实体))
{
返回“实体”;
}
其他的
{
抛出新异常(“Type”+Type.FullName+“不包含EntityLogicalName字段”);
}
}
返回(字符串)字段。GetValue(null);
}

此实例中的m_上下文是什么?它是ServiceContext类型吗?此外,该方法是否将位于特定于实体的某种形式的存储库类中?例如,声明为存储库或其他什么?