【问题标题】:Why is my OData V4 Identifier not working on my ASP.NET dataservice?为什么我的 OData V4 标识符在我的 ASP.NET 数据服务上不起作用?
【发布时间】:2016-12-01 09:52:56
【问题描述】:

在使用整数键时,我在 ASP.NET 中使用 OData V4 服务时遇到问题。我没有使用实体框架,因为我从 SOAP 服务获取数据。

这是我的数据类:

public class RecipeDto
{
    public RecipeDto();
    public RecipeDto(string name);
    public RecipeDto(int ident);

    public string Description { get; set; }
    public int Ident { get; set; }
    public string Name { get; set; }
    public List<RecipeVersionDto> Versions { get; set; }
}

我使用 fluent API 设置我的密钥:

var rtdto = builder.EntitySet<RecipeTemplateDto>("AgentConfigTemplates").EntityType
                .HasKey(r => r.Ident)
                .HasMany(r => r.Versions);

这是我的服务元数据:

<EntityType Name="RecipeTemplateDto">
<Key>
<PropertyRef Name="Ident"/>
</Key>
<Property Name="Ident" Type="Edm.Int32" Nullable="false"/>
<Property Name="Description" Type="Edm.String"/>
<Property Name="Name" Type="Edm.String"/>
<NavigationProperty Name="Versions" Type="Collection(Ifmdatalink.Linerecorder.Backend.PlugIn.dto.RecipeTemplateVersionDto)"/>
</EntityType>

现在我希望通过使用此查询获得我的实体集的第一个条目:

GET http://localhost:13917/my.svc/AgentConfigTemplates(1)

但我总是得到完整的清单。

为什么会发生这种情况,我怎样才能获得第一个条目?

我是否必须以某种方式扩展我的 odata 控制器?

如果我将密钥放在引号中,我会收到错误的请求响应。

【问题讨论】:

    标签: asp.net odata .net-4.6.1


    【解决方案1】:

    答案既不在模型中,也不在 fluent api 定义中。这是控制器的问题。一个 OData 控制器需要实现两个 get 方法:

    public async Task<IQueryable<AgentVersionDto>> Get()
    
    public SingleResult<AgentDto> Get([FromODataUri] int key)
    

    这涵盖了整个实体集和单个条目的获取。如果后者未实现,webapi 2 中的 odata 服务将始终返回整个列表。

    【讨论】:

      猜你喜欢
      • 2014-01-26
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多