C# 早恋
当我将注释链接到特定实体,而不是创建这样的关系时:C# 早恋,c#,.net,visual-studio,c#-4.0,dynamics-crm-2011,C#,.net,Visual Studio,C# 4.0,Dynamics Crm 2011,当我将注释链接到特定实体,而不是创建这样的关系时: var associateRequest = new AssociateRequest { Target = new EntityReference(SalesOrder.EntityLogicalName, salesOrderGuid), RelatedEntities = new EntityReferenceCollection { new EntityReference(Annotation.E
var associateRequest = new AssociateRequest
{
Target = new EntityReference(SalesOrder.EntityLogicalName, salesOrderGuid),
RelatedEntities = new EntityReferenceCollection
{
new EntityReference(Annotation.EntityLogicalName, noteGuid),
},
Relationship = new Relationship("SalesOrder_Annotation")
};
是否可以以强类型方式引用关系:
var associateRequest = new AssociateRequest
{
Target = new EntityReference(SalesOrder.EntityLogicalName, salesOrderGuid),
RelatedEntities = new EntityReferenceCollection
{
new EntityReference(Annotation.EntityLogicalName, noteGuid)
},
Relationship = SalesOrder.Relationships.SalesOrder_Annotation // <----- ???
};
SalesOrder.Relationships.SalesOrder_Annotation
我们能否以同样的方式引用特定的1:N关系:
var associateRequest = new AssociateRequest
{
Target = new EntityReference(SalesOrder.EntityLogicalName, salesOrderGuid),
RelatedEntities = new EntityReferenceCollection
{
new EntityReference(Annotation.EntityLogicalName, noteGuid)
},
Relationship = SalesOrder.Relationships.SalesOrder_Annotation // <----- ???
};
SalesOrder.Relationships.SalesOrder_Annotation
根据您所说的,您生成的类具有一个属性,该属性具有您的关系名称。
您只需要一个t4模板,它为您的关系生成一个具有强类型属性的类 假设您的项目中有以下代码
namespace ConsoleApplication9
{
public class RelationshipAttribute : System.Attribute
{
public string Name { get; set; }
public RelationshipAttribute(string name) { Name = name; }
}
[Relationship("RelationShipA")]
public class Class1 { }
[Relationship("RelationShipB")]
public class Class2 { }
[Relationship("RelationShipC")]
public class Class3 { }
}
此模板
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(TargetPath)" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Reflection" #>
<#@ output extension=".cs" #>
namespace YourNameSpace
{
public static class Relationships
{
<#
var types = typeof(ConsoleApplication9.RelationshipAttribute).Assembly
.GetTypes()
.Select(t => new { t.Name, Value = t.GetCustomAttribute(typeof(ConsoleApplication9.RelationshipAttribute)) })
.Where(t => t.Value != null)
.Select(t=> new { t.Name,Value= ((ConsoleApplication9.RelationshipAttribute)t.Value).Name })
.ToList();
foreach (var attr in types)
{ #>
public static class <#= attr.Name #>
{
public const string <#= attr.Value #> = "<#= attr.Value #>";
}
<# }
#>}
}
然后你可以像这样使用它
Relationship = new Relationship(Relationships.Class1.RelationShipA )
如果您使用SDK中提供的标准
CrmSvcUtil.exe
应用程序生成代码(\SDK\Bin\CrmSvcUtil.exe
),则要查找的值将存储在代码属性RelationshipSchemaNameAttribute
中。我已经使用SDK中提供的早期绑定实体类文件在控制台应用程序中验证了此代码(\SDK\SampleCode\CS\HelperCode\MyOrganizationCrmSdkTypes.CS
)
按如下方式调用该方法(根据您的示例):
var relationship=GetRelationship(nameof(salesforder.salesforder\u注释))
或者,如果要返回实际字符串值:
var relationshipName=GetRelationshipSchemaName(nameof(salesforder.salesforder\u注释))
将以下代码添加到应用程序中的帮助器类:
public static string GetRelationshipSchemaName<T>(string relationshipPropertyName) where T:Entity
{
return typeof (T).GetProperties()
.FirstOrDefault(x => x.Name == relationshipPropertyName)
.GetCustomAttributes()
.OfType<RelationshipSchemaNameAttribute>()
.FirstOrDefault()
?.SchemaName;
}
public static Relationship GetRelationship<T>(string relationshipPropertyName) where T : Entity
{
return new Relationship(typeof(T).GetProperties()
.FirstOrDefault(x => x.Name == relationshipPropertyName)
.GetCustomAttributes()
.OfType<RelationshipSchemaNameAttribute>()
.FirstOrDefault()
?.SchemaName);
}
公共静态字符串GetRelationshipSchemaName(字符串relationshipPropertyName),其中T:Entity
{
返回typeof(T).GetProperties()
.FirstOrDefault(x=>x.Name==relationshipPropertyName)
.GetCustomAttributes()
第()类
.FirstOrDefault()
?SchemaName;
}
公共静态关系GetRelationship(string relationshipPropertyName),其中T:Entity
{
返回新关系(typeof(T).GetProperties()
.FirstOrDefault(x=>x.Name==relationshipPropertyName)
.GetCustomAttributes()
第()类
.FirstOrDefault()
?SchemaName);
}
这是更新后的代码的外观:
var associateRequest = new AssociateRequest
{
Target =
new EntityReference(
SalesOrder.EntityLogicalName,
salesOrderGuid),
RelatedEntities =
new EntityReferenceCollection
{
new EntityReference(
Annotation
.EntityLogicalName,
noteGuid)
},
Relationship = GetRelationship<SalesOrder>(nameof(SalesOrder.SalesOrder_Annotation)) ///////////????
};
var associateRequest=新的associateRequest
{
目标=
新实体参照(
SalesOrder.EntityLogicalName,
salesOrderGuid),
相关实体=
新实体引用集合
{
新实体参照(
注释
.EntityLogicalName,
noteGuid)
},
Relationship=GetRelationship(nameof(salesforder.salesforder\u注释))////????
};
仔细想想,我的t4模板答案似乎有些过火您可以使用表达式树和扩展方法轻松获得所需内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace ConsoleApplication9
{
internal class Program
{
private static void Main(string[] args)
{
Relationship r = new Class1().GetRelationShip(s => s.RelationShipProperty);
Console.WriteLine(r.Name);
System.Console.ReadLine();
}
}
public static class MyExtention
{
public static Relationship GetRelationShip<T, TProperty>(this T t, Expression<Func<T, TProperty>> expression)
{
return new Relationship(((expression.Body as MemberExpression).Member as PropertyInfo)
.GetCustomAttributes(typeof(RelationshipAttribute))
.Select(a=>(RelationshipAttribute)a)
.First().Name
);
}
}
public class RelationshipAttribute : System.Attribute
{
public string Name { get; set; }
public RelationshipAttribute(string name)
{
Name = name;
}
}
public class Relationship
{
public string Name { get; set; }
public Relationship(string name)
{
Name = name;
}
}
public class Class1
{
[Relationship("RelationShipA")]
public List<int> RelationShipProperty { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Linq.Expressions;
运用系统反思;
命名空间控制台应用程序9
{
内部课程计划
{
私有静态void Main(字符串[]args)
{
关系r=new Class1().GetRelationShip(s=>s.RelationShipProperty);
控制台写入线(r.Name);
System.Console.ReadLine();
}
}
公共静态类MyExtension
{
公共静态关系GetRelationShip(此T,表达式)
{
返回新关系(((expression.Body作为MemberExpression.Member作为PropertyInfo)
.GetCustomAttributes(typeof(RelationshipAttribute))
.选择(a=>(RelationshipAttribute)a)
.First().Name
);
}
}
公共类RelationshipAttribute:System.Attribute
{
公共字符串名称{get;set;}
公共关系属性(字符串名称)
{
名称=名称;
}
}
公共阶级关系
{
公共字符串名称{get;set;}
公共关系(字符串名称)
{
名称=名称;
}
}
公共班级1
{
[关系(“关系”)]
公共列表关系属性{get;set;}
}
}
我不确定我的问题是否正确。
C#6.0的特性nameof(…)
不会做这件事吗
即
XrmToolBox中的早期绑定生成器将为您生成这些关系名称
public static class Fields
{
public const string AccountId = "accountid";
public const string AccountRoleCode = "accountrolecode";
public const string Address1_AddressId = "address1_addressid";
// *** SNIP ***
public const string YomiLastName = "yomilastname";
public const string YomiMiddleName = "yomimiddlename";
public const string business_unit_contacts = "business_unit_contacts";
public const string contact_customer_accounts = "contact_customer_accounts";
public const string Referencingcontact_customer_contacts = "contact_customer_contacts";
public const string Referencingcontact_master_contact = "contact_master_contact";
public const string contact_owning_user = "contact_owning_user";
public const string lk_contact_createdonbehalfby = "lk_contact_createdonbehalfby";
public const string lk_contact_modifiedonbehalfby = "lk_contact_modifiedonbehalfby";
public const string lk_contactbase_createdby = "lk_contactbase_createdby";
public const string lk_contactbase_modifiedby = "lk_contactbase_modifiedby";
public const string system_user_contacts = "system_user_contacts";
}
SalesOrder.Relationships.SalesOrder\u注释返回的类型是什么?如果它不是
关系
,那么就不能调用它。我必须问一下,因为标准的CrmSvcUtil.exe代码生成工具无法使用SalesOrder.Relationships.SalesOrder\u注释
,所以它可能是自定义的。使用CrmSvcUtil可用的任何关系是否可以使用entity.relationship名称或类似的名称调用?否。您必须创建一个扩展来生成带有关系名称的const
或readonly
字符串。或者编写一个方法,从CrmSvcUtil.exe
@Nicknow-got-examples?:)输出的代码属性中读取关系名称@l-“你使用过任何答案吗?”“下一个投票人”不确定这个答案值得下一个投票…它可能比其他答案更复杂,但另一方面它没有性能惩罚。。。如果OP想要使用代码,那么它可能是问题的最佳答案(从我的
public static class Fields
{
public const string AccountId = "accountid";
public const string AccountRoleCode = "accountrolecode";
public const string Address1_AddressId = "address1_addressid";
// *** SNIP ***
public const string YomiLastName = "yomilastname";
public const string YomiMiddleName = "yomimiddlename";
public const string business_unit_contacts = "business_unit_contacts";
public const string contact_customer_accounts = "contact_customer_accounts";
public const string Referencingcontact_customer_contacts = "contact_customer_contacts";
public const string Referencingcontact_master_contact = "contact_master_contact";
public const string contact_owning_user = "contact_owning_user";
public const string lk_contact_createdonbehalfby = "lk_contact_createdonbehalfby";
public const string lk_contact_modifiedonbehalfby = "lk_contact_modifiedonbehalfby";
public const string lk_contactbase_createdby = "lk_contactbase_createdby";
public const string lk_contactbase_modifiedby = "lk_contactbase_modifiedby";
public const string system_user_contacts = "system_user_contacts";
}