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

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

我正在使用以下元数据访问我的 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"
                }
            ]
        }
    ]
}

当我尝试使用 Simple.Odata.Client 获取 ValuesContainer 时,我收到以下错误:

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,
            };
        }
...
}

我的元数据有误吗and/or有解决办法吗?它并不是真正需要解决它,但是在运行时抛出那么多异常会导致过多的开销,因为这些是昂贵的操作。

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

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

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

所以根据 Echamus 原文的错误 post:

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

为我修复它的解决方案是确保为“values”包含的实体类型定义了密钥(在本例中,Value型号配置):

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
        }
    }
}

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

如果您已经定义了这个,但是您有其他配置出现 before/after 它,那些其他配置可能是问题所在,并且可能导致此特定行静默失败。

希望这有助于在将来为某人节省一些时间。

我遇到了这个错误,我通过设置解决了它:

MergeOption = MergeOption.NoTracking;

在我的客户端上,因为我当时只进行 read-only 操作。

在此处找到答案:https://github.com/OData/odata.net/issues/2211