如何在odata 4开放类型中访问复杂类型属性?

时间:2019-03-19 09:49:36

标签: odata

我有一个Entity类型,它是这样声明的开放类型:

    public class PseudoValue
    {
        public string Value { get; set; }
    }

   public class Entity
    {
        [Key]
        public ulong Id { get; set; }

        public IDictionary<string, object> DynamicProperties { get; set; }
    }

在此过程中的某处,我向我的实体添加了一个动态属性,如下所示:

propertiesMap.TryAdd("explicitAdd", new PseudoValue { Value = "Types" });

当我尝试像这样查询我的数据

~/api/Entities/?$filter=explicitAdd/App.PseudoValue/Value eq 'Types'

我有例外

URI中指定的查询无效。遇到无效的类型转换。无法从“ ”分配“ App.PseudoValue”。

这似乎源于强制转换无法工作的事实,因为属性explicitAdd的类型是开放类型,并且正在UriEdmHelpers.cs中从该函数抛出

        /// <summary>
        /// Check whether the parent and child are properly related types
        /// </summary>
        /// <param name="parentType">the parent type</param>
        /// <param name="childType">the child type</param>
        /// <exception cref="ODataException">Throws if the two types are not related.</exception>
        public static void CheckRelatedTo(IEdmType parentType, IEdmType childType)
        {
            if (!IsRelatedTo(parentType, childType))
            {
                // If the parentType is an open property, parentType will be null and can't have an ODataFullName.
                string parentTypeName = (parentType != null) ? parentType.FullTypeName() : "<null>";
                throw new ODataException(Strings.MetadataBinder_HierarchyNotFollowed(childType.FullTypeName(), parentTypeName));
            }
        }

如果相反,我会这样添加我的媒体资源:

 propertiesMap.TryAdd("thisWorks", "Types");

此查询成功:

~/api/Entities/?$filter=thisWorks eq 'Types'

有什么办法可以避免类型检查,以便第一个示例可以这样写?

~/api/Entities/?$filter=explicitAdd/Value eq 'Types'

0 个答案:

没有答案
相关问题