首页 > 解决方案 > 不能将 $count 与 ODataRoute 属性一起使用

问题描述

使用ODataRoute属性会使$count不起作用。

/// Startup.cs
app.UseOData("odata", "odata", GetEdmModel());

static GetEdmModel()
{
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<User>("User");
    builder.EntitySet<User>("Product");
    return builder.GetEdmModel();
}
/// MyController.cs
[EnableQuery]
[ODataRoute("User")]
public IQueryable<User> GetUser() => User.GetQuery());

[EnableQuery]
[ODataRoute("Product")]
public IQueryable<Product> GetProduct() => Product.GetQuery());
http://localhost:5901/odata/User(工程)
http://localhost:5901/odata/User/$count(不工作)

标签: c#.net.net-coreodata

解决方案


我认为将 AllowedQueryOptions 添加到EnableQuery属性可能会有所帮助

[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All )]
[ODataRoute("User")]
public IQueryable<User> GetUser() => User.GetQuery());

推荐阅读