l--*_*''' 14 .net c# visual-studio c#-4.0 dynamics-crm-2011
当我将注释链接到特定实体时,而不是像这样创建一个关系:
var associateRequest = new AssociateRequest
{
Target = new EntityReference(SalesOrder.EntityLogicalName, salesOrderGuid),
RelatedEntities = new EntityReferenceCollection
{
new EntityReference(Annotation.EntityLogicalName, noteGuid),
},
Relationship = new Relationship("SalesOrder_Annotation")
};
Run Code Online (Sandbox Code Playgroud)
是否可以以强类型方式引用关系:
var associateRequest = new AssociateRequest
{
Target = new EntityReference(SalesOrder.EntityLogicalName, salesOrderGuid),
RelatedEntities = new EntityReferenceCollection
{
new EntityReference(Annotation.EntityLogicalName, noteGuid)
},
Relationship = SalesOrder.Relationships.SalesOrder_Annotation // <----- ???
};
Run Code Online (Sandbox Code Playgroud)
这类似于能够在开发时获取逻辑名称:
SalesOrder.EntityLogicalName
Run Code Online (Sandbox Code Playgroud)
我们可以用同样的方式引用特定的1:N关系:
SalesOrder.Relationships.SalesOrder_Annotation
Run Code Online (Sandbox Code Playgroud)
RelationshipSchemaNameAttribute
如果使用CrmSvcUtil.exe
SDK(\SDK\Bin\CrmSvcUtil.exe
)中提供的标准应用程序生成代码,则您要查找的值将存储在代码属性中.我已经使用SDK(\SDK\SampleCode\CS\HelperCode\MyOrganizationCrmSdkTypes.cs
)中提供的早期绑定实体类文件在控制台应用程序中验证了此代码.
按如下方式调用方法(根据您的示例):
var relationship = GetRelationship<SalesOrder>(nameof(SalesOrder.SalesOrder_Annotation))
或者,如果要返回实际的字符串值:
var relationshipName = GetRelationshipSchemaName<SalesOrder>(nameof(SalesOrder.SalesOrder_Annotation))
将此代码添加到应用程序中的帮助程序类:
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);
}
Run Code Online (Sandbox Code Playgroud)
这是您更新的代码的样子:
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)) ///////////????
};
Run Code Online (Sandbox Code Playgroud)
第二个想法,我的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; }
}
}
Run Code Online (Sandbox Code Playgroud)
根据您的说法,您生成的类具有一个带有关系名称的属性。
您所需要的只是一个 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 { }
}
Run Code Online (Sandbox Code Playgroud)
这个模板
<#@ 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 #>";
}
<# }
#>}
}
Run Code Online (Sandbox Code Playgroud)
将生成以下 .cs 文件
namespace YourNameSpace
{
public static class Relationships
{
public static class Class1
{
public const string RelationShipA = "RelationShipA";
}
public static class Class2
{
public const string RelationShipB = "RelationShipB";
}
public static class Class3
{
public const string RelationShipC = "RelationShipC";
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样使用它
Relationship = new Relationship(Relationships.Class1.RelationShipA )
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
964 次 |
最近记录: |