【发布时间】:2021-05-04 10:49:36
【问题描述】:
有没有办法在 Edm 模型生成期间向 $metadata 添加复杂的自定义注释?
示例(下面的 XML 由 .NET 自动生成):
<EntitySet Name="Persons" EntityType="MyNS.Person">
<NavigationPropertyBinding Path="Contracts" Target="Contracts"/>
<NavigationPropertyBinding Path="CreatedBy" Target="Users"/>
<NavigationPropertyBinding Path="UpdatedBy" Target="Users"/>
<Annotation Term="Org.OData.Capabilities.V1.ExpandRestrictions">
<Record>
<PropertyValue Property="Expandable" Bool="true"/>
<PropertyValue Property="NonExpandableProperties">
<Collection>
<NavigationPropertyPath>Contacts</NavigationPropertyPath>
</Collection>
</PropertyValue>
</Record>
</Annotation>
</EntitySet>
我想添加一个复杂的注释(这是 SAPUI5 词汇表的一部分):
<EntitySet Name="Persons" EntityType="MyNS.Person">
<NavigationPropertyBinding Path="Contracts" Target="Contracts"/>
<NavigationPropertyBinding Path="CreatedBy" Target="Users"/>
<NavigationPropertyBinding Path="UpdatedBy" Target="Users"/>
<Annotation Term="Org.OData.Capabilities.V1.ExpandRestrictions">
<Record>
<PropertyValue Property="Expandable" Bool="true"/>
<PropertyValue Property="NonExpandableProperties">
<Collection>
<NavigationPropertyPath>Contacts</NavigationPropertyPath>
</Collection>
</PropertyValue>
</Record>
</Annotation>
<!--
↓↓ This part ↓↓
-->
<Annotation Term="UI.LineItem">
<Collection>
<Record Type="UI.DataField">
<PropertyValue Property="Value" Path="Name"/>
<Annotation Term="UI.Importance" EnumMember="UI.ImportanceType/High"/>
</Record>
</Collection>
</Annotation>
</EntitySet>
到目前为止,我已经能够使用以下代码向 EdmElement 添加自定义属性:
var personElement = oEdmModel.EntityContainer.Elements.First();
var stringType = EdmCoreModel.Instance.GetString(true);
var value = new EdmStringConstant(stringType, "my custom value");
oEdmModel.SetAnnotationValue(personElement , "MyNS", "Name", value);
或
oEdmModel.DirectValueAnnotationsManager.SetAnnotationValue(a, "MyNamespace", "AdminOnly", new EdmBooleanConstant(EdmCoreModel.Instance.GetBoolean(false), true));
(来源:How do you declare a custom oData annotation)
...但是找不到添加“复杂”注释类型的方法。
感谢您的帮助!
【问题讨论】:
标签: c# .net-core annotations odata asp.net-core-webapi