【问题标题】:Association [values] was not found upon using Simple.OData.Client使用 Simple.OData.Client 时未找到关联 [值]
【发布时间】:2021-11-19 06:11:05
【问题描述】:

我有一个通过 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' em>

我的 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 的不当行为?

【问题讨论】:

    标签: c# .net-core odata simple.odata.client


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多