.Net Core OData enable filtering for single entity set

*爱你&永不变心* 提交于 2020-01-15 15:27:07

问题


I'm running the Beta of OData on .Net Core. I'm trying to only enable certain features of odata depending on the controller. My startup class looks like so:

services.AddOData();   
//...

app.UseMvc(routeBuilder =>
{
    //routeBuilder.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
    routeBuilder.MapODataServiceRoute("odata", null, GetModel());
    routeBuilder.EnableDependencyInjection();
});

public static IEdmModel GetModel()
{
    var builder = new ODataConventionModelBuilder();
    var skillSet = builder.EntitySet<Skill>(nameof(Skill));
    skillSet.EntityType.Count().Filter().OrderBy().Expand().Select();
    builder.Namespace = "ODataTest.Models";
    builder.ContainerName = "DefaultContainer";

    return builder.GetEdmModel();
}

When I globally enable odata everything works fine. However, I cannot expose all of my entities

routeBuilder.Count().Filter().OrderBy().Expand().Select().MaxTop(null);

but when, I try to enable odata for a specific entity set my I get an error when I attempt to filter

skillSet.EntityType.Count().Filter().OrderBy().Expand().Select();

The query specified in the URI is not valid. The property 'Name' cannot be used in the $filter query option.

For the sake of completeness here is my controller:

[HttpGet]
[EnableQuery]
public async Task<Skill[]> GetFilteredODataList(ODataQueryOptions<Skill> q)
{
    var skillsQuery = this._context.Skills.AsQueryable();
    if (q?.Filter != null)
    {
        skillsQuery = q.Filter.ApplyTo(skillsQuery, new ODataQuerySettings()) as IQueryable<Skill>;
    }

    return await skillsQuery.ToArrayAsync();
}

回答1:


I wonder if your issue could be solved by the way ModelBuilder.cs is built in my SO question? In that page you should be able to define per entity what OData settings are permitted.

The article I link to in the first paragraph will explain it better than I did as that was what I was following.



来源:https://stackoverflow.com/questions/49478028/net-core-odata-enable-filtering-for-single-entity-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!