Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 由于导航源';价值观';无法解析为模型中的已知实体集_C#_Asp.net_.net_Odata - Fatal编程技术网

C# 由于导航源';价值观';无法解析为模型中的已知实体集

C# 由于导航源';价值观';无法解析为模型中的已知实体集,c#,asp.net,.net,odata,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" Namespa

我正在使用以下元数据访问我的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>
当我试图通过使用
Simple.Odata.Client
获取
ValuesContainer
时,我收到以下错误:

Microsoft.OData.ODataException:'无法计算Id,因为无法将导航源“值”解析为模型中的已知实体集。

引发异常的部分:

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

我的元数据是否错误和/或是否有解决方法?实际上并不需要解决这个问题,但是在运行时抛出这么多异常会导致太多开销,因为这些都是昂贵的操作

找到了解决方案,不得不将
包含的
属性添加到我的值中。

我也被这个特定的错误所困扰。但设置包含属性不是我们的选择。发布此消息以防其他人遇到此问题

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

因此,根据Echamus的原始帖子中的错误:

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

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

使用Microsoft.AspNet.OData.Builder;
使用Microsoft.AspNetCore.Mvc;
使用MyProject.Api.Models;
命名空间MyProject.Api.Configuration.Model\u配置
{
公共类ValueModelConfiguration:IModelConfiguration
{
公共无效应用(ODataModelBuilder builder,ApiVersion)
{
builder.EntitySet(nameof(Value)).EntityType.HasKey(v=>v.id);
//您实体的其他配置(如价值)可能在此处
}
}
}
(注:以上值为原始错误中投诉的“导航源”的任何实体)

如果您已经定义了这个,但是在它之前/之后发生了其他配置,那么这些其他配置可能就是问题所在,并且可能导致这条特定的线路以静默方式失败

希望这有助于节省一些人在未来的时间

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