繁体   English   中英

oData筛选器不适用于MongoDB和Web API的导航属性

[英]oData filter not working on navigation property with MongoDB and Web API

控制器看起来像

    public class NodesRestController : ODataController
{
    private INodeService _nodeService;
    public NodesRestController(INodeService nodeService)
    {
        _nodeService = nodeService;
    }
    [EnableQuery()]
    public IQueryable<Node> Get()
    {
        return _nodeService.GetAllNodes();
    }
    [EnableQuery()]
    public Node Get(string id)
    {
        return _nodeService.GetNodeById(id);
    }
}

在MongoDb存储库中,我返回集合的AsQueryable。

//..............Rest of initializations



 _collection = _dbContext.Database
            .GetCollection<TEntity>(typeof(TEntity).Name);
//..........

    public IQueryable<TEntity> GetAll()
    {
        return _collection.AsQueryable();
    }



public TEntity Insert(TEntity entity)
    {
        entity.Id = ObjectId.GenerateNewId().ToString();
         _collection.Insert(entity);
        return entity;
    }

    //..............Rest of initializations

MongoDB文档看起来像

{
"_id" : "5688d5b1d5ae371c60ffd8ef",
"Name" : "RTR1",
"IP" : "1.2.2.22",
"NodeGroup" : {
    "_id" : "5688d5aad5ae371c60ffd8ee",
    "Name" : "Group One",
    "Username" : null,
    "Password" : null
}}

使用ObjectId.GenerateNewId()。ToString()生成ID,因此它们以字符串形式存储。

Node和NodeGroup是纯POCO

public partial class NodeGroup : EntityBase
{
    public string Name { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string LoginPrompt { get; set; }
    public string PasswordPrompt { get; set; }
    public string ReadyPrompt { get; set; }
    public string Description { get; set; }
}
 public partial class Node : EntityBase
{
    public string Name { get; set; }
    public string IP { get; set; }
    public virtual NodeGroup NodeGroup { get; set; }
}
public abstract class EntityBase 
{
    //[JsonIgnore]
    // [BsonRepresentation(BsonType.ObjectId)]
    // [BsonId]
    public string Id { get; set; }
}

问题

oData URI,例如

http://localhost:9910/api/NodesRest
http://localhost:9910/api/NodesRest?$expand=NodeGroup
http://localhost:9910/api/NodesRest?$expand=NodeGroup&$filter=Name eq 'RTR1'

工作正常。

但是当我尝试过滤导航属性时

http://localhost:9910/api/NodesRest?$expand=NodeGroup&$filter=NodeGroup/Name eq 'Group One'

它给了我例外

消息:“无法确定表达式的序列化信息:ConditionalExpression。”,

我用了_collection.FindAll(); 而且有效。

当我不使用mongoDB进行内存收集时,它为我工作。 也一样

mongodb的c#驱动程序中的AsQueryable方法有点问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM