Search code examples
c#asp.net.netodata

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


I am accessing my OData service with following metadata (simplified and obfuscated to the relevant part), this is generated by using 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>

Some example the output it generates:

{
    "@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"
                }
            ]
        }
    ]
}

When I try to get a ValuesContainer via using the Simple.Odata.Client I receive the following error:

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

Part where the exception is thrown:

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

Is my metadata wrong and/or is there a workaround for this? It's not really required to solve it, but having that many exceptions thrown during runtime causes too much overhead as these are expensive operations.


Solution

  • Found the solution, had to add the Contained attribute to my Value.