【问题标题】:Microsoft.AspNetCore.OData - Custom annotations $metadataMicrosoft.AspNetCore.OData - 自定义注释 $metadata
【发布时间】: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


    【解决方案1】:
    EdmModel model = new EdmModel();
    
    // Entity Type
    EdmEntityType person = new EdmEntityType("NS", "Person");
    model.AddElement(person);
    
    // EntityContainer
    EdmEntityContainer container = new EdmEntityContainer("NS", "Default");
    EdmEntitySet persons = container.AddEntitySet("Persons", person);
    model.AddElement(container);
    
    // Complex Type
    EdmComplexType dataField = new EdmComplexType("UI", "DataField");
    dataField.AddStructuralProperty("Value", EdmCoreModel.Instance.GetAnnotationPath(true));
    model.AddElement(dataField);
    
    // Term Type
    EdmTerm term = new EdmTerm("UI", "LineItem", new EdmComplexTypeReference(dataField, true));
    model.AddElement(term);
    
    EdmRecordExpression record = new EdmRecordExpression(new EdmPropertyConstructor("Value", new EdmPathExpression("Name")));
    EdmVocabularyAnnotation annotation = new EdmVocabularyAnnotation(persons, term, record);
    annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); // no necessary
    model.SetVocabularyAnnotation(annotation);
    

    然后可以调用“CsdlWriter.TryWriteCsdl”编写CSDL并验证为:

    <?xml version="1.0" encoding="utf-16"?>
    <edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
      <edmx:DataServices>
        <Schema Namespace="NS" xmlns="http://docs.oasis-open.org/odata/ns/edm">
          <EntityType Name="Person" />
          <EntityContainer Name="Default">
            <EntitySet Name="Persons" EntityType="NS.Person">
              <Annotation Term="UI.LineItem">
                <Record>
                  <PropertyValue Property="Value" Path="Name" />
                </Record>
              </Annotation>
            </EntitySet>
          </EntityContainer>
        </Schema>
        <Schema Namespace="UI" xmlns="http://docs.oasis-open.org/odata/ns/edm">
          <ComplexType Name="DataField">
            <Property Name="Value" Type="Edm.AnnotationPath" />
          </ComplexType>
          <Term Name="LineItem" Type="UI.DataField" />
        </Schema>
      </edmx:DataServices>
    </edmx:Edmx>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 2015-09-15
      • 1970-01-01
      相关资源
      最近更新 更多