0

我有一个通过IModelConfiguration配置的 OData 服务

public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
{
     builder.EntitySet<ValuesContainer>("ValuesContainer");
     builder.AddComplexType(typeof(ValueItem));
}

这会产生以下(混淆并选择相关部分)元数据:

<Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
    <DataServices>
        <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="MyProject.Api.Models">
            <EntityType Name="ValuesContainer">
                <Key>
                    <PropertyRef Name="id" />
                </Key>
                <Property Name="values" Type="Collection(MyProject.Api.Models.ValueItem)" />
                <Property Name="id" Type="Edm.Guid" Nullable="false" />
            </EntityType>
            <ComplexType Name="ValueItem">
                <Property Name="value" Type="Edm.String" />
                <Property Name="id" Type="Edm.Guid" Nullable="false" />
            </ComplexType>
        </Schema>
   </DataServices>
</Edmx>

基本上我有一个实体类型ValuesContainer,它有一个复杂类型ValueItem的集合。

当我尝试通过 http get 查询服务时,以下示例工作正常:

~/odata/valuescontainer?$filter=values/any(v:v/value eq 'example')

这个例子给了我任何ValuesContainer,它有一个包含例子的值。

然而,当我在另一个 C# 应用程序中使用Simple.OData.Client时,我收到错误Simple.OData.Client.UnresolvableObjectException: 'Association [values] not found'

我的 Simple.OData.Client 的代码:

var httpClient = _httpClientFactory.CreateClient();
var odataClientSettings = new ODataClientSettings(httpClient)
{
    BaseUri = new Uri($"{_myEndPointConfig.BaseUrl}/odata/")
};
var entries = await new ODataClient(odataClientSettings).For<ValuesContainer>("ValuesContainer")
                .Filter(x => x.Values.Any(v => v.value == "example").FindEntriesAsync().ConfigureAwait(false);

我试图通过 Fiddler 追踪请求是否错误,但它发生在接收 OData 服务的元数据和处理表达式之间的某个地方。

我注意到它在以下位置引发异常

namespace Simple.OData.Client.V4.Adapter
{
    public class Metadata : MetadataBase {
    ...
        private IEdmNavigationProperty GetNavigationProperty(string collectionName, string propertyName)
        {
            var property = GetEntityType(collectionName).NavigationProperties()
                .BestMatch(x => x.Name, propertyName, NameMatchResolver);

            if (property == null)
                // obviously being thrown here
                throw new UnresolvableObjectException(propertyName, $"Association [{propertyName}] not found");

            return property;
        }
    ...
    }
}

它试图将我的属性Values作为NavigationProperty,但它不是一个。

我的元数据和我的 OData 服务的配置是否错误,为什么它通过 http get 调用工作,或者这是 Simple.OData.Client 的不当行为?

4

1 回答 1

0

显然 Simple.OData.Client 中有一个错误,它要求集合具有NavigationProperty,这是不正确的。

https://github.com/simple-odata-client/Simple.OData.Client/issues/274

于 2020-02-12T09:28:43.237 回答