为什么我的OData V4标识符不能用于我的ASP.NET数据服务?

时间:2016-12-01 09:52:56

标签: asp.net odata-v4 .net-4.6.1

使用整数键时,我在ASP.NET中使用OData V4服务时出现问题。我没有使用Entity Framework,因为我从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; }
}

我使用流畅的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控制器?

如果我把我的密钥放在引号中,我会收到错误的请求回复。

1 个答案:

答案 0 :(得分:1)

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

^(\d+) (\d+)$[.\s]+?^\1 (\d+)

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