コントローラは次のようになります
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
}}
ID は ObjectId.GenerateNewId().ToString() を使用して生成されたため、文字列として格納されます。
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'
正常に動作します。
しかし、Navigation プロパティでフィルタリングしようとすると
http://localhost:9910/api/NodesRest?$expand=NodeGroup&$filter=NodeGroup/Name eq 'Group One'
それは私に例外を与えます
メッセージ: "式のシリアル化情報を特定できません: ConditionalExpression.",