无法计算ID,因为无法将导航源“值”解析为模型中的已知实体集

时间:2020-02-12 12:43:42

标签: c# asp.net .net odata

我正在使用以下元数据(简化并混淆到相关部分)访问OData服务,这是通过使用Microsoft.AspNet.OData生成的:

<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="id" Type="Edm.Guid" Nullable="false" />
                <NavigationProperty Name="values" Type="Collection(MyProject.Api.Models.Value)"/>
            </EntityType>
            <EntityType Name="Value">
                <Key>
                    <PropertyRef Name="id"/>
                </Key>
                <Property Name="value" Type="Edm.String" />
                <Property Name="id" Type="Edm.Guid" Nullable="false" />
                <Property Name="valuesContainerId" Type="Edm.Guid"/>
                <NavigationProperty Name="valuesContainer" Type="MyProject.Api.Models.ValuesContainer">
                    <ReferentialConstraint Property="valuesContainerId" ReferencedProperty="id"/>
                </NavigationProperty>
            </EntityType>
        </Schema>
        </DataServices>
</Edmx>

一些示例,它生成的输出:

{
    "@odata.context": "https://localhost:5002/v1/odata/$metadata#ValuesContainer(values())",
    "value": [
        {
            "id": "2996e6ea-3e72-4b4c-8b3b-b076e34f6dac",
            "values": [
                {
                    "value": "Hello world",
                    "valuesContainerId": "2996e6ea-3e72-4b4c-8b3b-b076e34f6dac",
                    "id": "3d10fcfa-27a2-4c21-7e01-08d783bf6c40"
                }
            ]
        }
    ]
}

当我尝试通过使用ValuesContainer获得Simple.Odata.Client时,出现以下错误:

Microsoft.OData.ODataException: 'The Id cannot be computed, since the navigation source 'values' cannot be resolved to a known entity set from model.'

引发异常的部分:

namespace Simple.OData.Client.V4.Adapter
{
    public class ResponseReader : ResponseReaderBase
...
private ODataEntryAnnotations CreateAnnotations(ODataResource odataEntry)
        {
            string id = null;
            Uri readLink = null;
            Uri editLink = null;
            if (_session.Adapter.GetMetadata().IsTypeWithId(odataEntry.TypeName))
            {
                try
                {
// Over here my exception occurs, calculating the odataEntry.Id.AbsoluteUri
                    id = odataEntry.Id.AbsoluteUri;
                    readLink = odataEntry.ReadLink;
                    editLink = odataEntry.EditLink;
                }
                catch (ODataException)
                {
/// Yep, the library contains this typo
                    // Ingored
                }
            }

            return new ODataEntryAnnotations
            {
                Id = id,
                TypeName = odataEntry.TypeName,
                ReadLink = readLink,
                EditLink = editLink,
                ETag = odataEntry.ETag,
                MediaResource = CreateAnnotations(odataEntry.MediaResource),
                InstanceAnnotations = odataEntry.InstanceAnnotations,
            };
        }
...
}

我的元数据是否错误和/或是否有解决方法?解决方案并不是真正需要它,但是在运行时抛出很多异常会导致过多的开销,因为这些操作很昂贵。

2 个答案:

答案 0 :(得分:1)

找到解决方案,必须将Contained属性添加到我的Value中。

答案 1 :(得分:0)

我也被这个特殊的错误所困扰。但是设置 Contained 属性对我们来说不是一个选项。发布此信息以防其他人遇到此问题。

我的问题是我的实体键的 IModelConfiguration 声明无声无息地失败(由于另一个配置设置试图错误地设置操作)。

因此基于 Echamus 原始帖子中的错误:

<块引用>

无法计算 Id,因为无法将导航源“”解析为模型中的已知实体集

为我修复它的解决方案是确保为“values”包含的实体类型定义了键(在本例中为ValueModelConfiguration):< /p>

using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNetCore.Mvc;
using MyProject.Api.Models;

namespace MyProject.Api.Configuration.Model_Configurations
{
    public class ValueModelConfiguration : IModelConfiguration
    {
        public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
        {
            builder.EntitySet<Value>(nameof(Value)).EntityType.HasKey(v => v.id);
            // other configurations for your entity (e.g. value) may be here
        }
    }
}

(注意:上面的是原始错误中所抱怨的“导航源”的任何实体)

如果您已经定义了它,但在它之前/之后发生了其他配置,那么这些其他配置可能是问题所在,并可能导致此特定行无声无息地失败。

希望能在未来帮助某人节省一些时间。