asp.netodata.net-4.6.1

Why is my OData V4 Identifier not working on my ASP.NET dataservice?


I have a Problem using my OData V4 Service in ASP.NET when using integer keys. I am not using Entity Framework, as I get my data from a SOAP service.

Here is my data class:

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

And I set my key using fluent API:

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

Here is my metadata on my service:

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

Now I would expect to get the first entry of my entity set by using this query:

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

But I always get the complete list.

Why is this happening and how can I get the first entry?

Do I have to extend my odata controller somehow?

If I put my key in quotes I get a bad request response.


Solution

  • The answer is neither in the model nor in the fluent api definition. It is a problem of the controller. An OData Controller needs to implement two get methods:

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

    This covers a get for the whole entity set and for a single entry. If the latter is not implemented the odata service in webapi 2 will always return the whole list.