我目前在自学OData,但遇到了一个我无法解决的情况。要么是我误解了OData规范,要么需要做些什么来使它正常工作。
我已经建立了一个小型的书籍和作者实体模型(EF/CF)。很简单的内容,从作者到书籍有一个一对多的关系:
我意识到可以通过查询“Books”实体来获取该信息...
这里是我的两个 OData 控制器,一个用于作者,另一个用于书籍:
所以,就像我所说的那样,还有其他需要添加或配置的内容来使关联实体中的引用属性工作吗?
我已经建立了一个小型的书籍和作者实体模型(EF/CF)。很简单的内容,从作者到书籍有一个一对多的关系:
modelBuilder.Entity<Book>().HasRequired(b => b.Author);
modelBuilder.Entity<Author>().HasMany(a => a.Books);
现在,当查询作者时,我希望能够展开书籍属性并过滤其(嵌套的)属性。例如,如果我这样问“谁写了哈利波特书”,...
http://myBooksDatabase/Authors?$expand=Books&$filter=contains(Books/Name,'Harry Potter')&$select=Name
我遇到了以下错误响应:
{
error: {
code: ""
message: "The query specified in the URI is not valid. The parent value for a property access of a property 'Name' is not a single value. Property access can only be applied to a single value."
innererror: {
message: "The parent value for a property access of a property 'Name' is not a single value. Property access can only be applied to a single value."
type: "Microsoft.OData.Core.ODataException"
stacktrace: " at Microsoft.OData.Core.UriParser.Parsers.EndPathBinder.BindEndPath(EndPathToken endPathToken) at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.BindEndPath(EndPathToken endPathToken) at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.Bind(QueryToken token) at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.BindFunctionParameter(FunctionParameterToken token) at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.Bind(QueryToken token) at Microsoft.OData.Core.UriParser.Parsers.FunctionCallBinder.<BindFunctionCall>b__8(FunctionParameterToken ar) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at Microsoft.OData.Core.UriParser.Parsers.FunctionCallBinder.BindFunctionCall(FunctionCallToken functionCallToken) at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.BindFunctionCall(FunctionCallToken functionCallToken) at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.Bind(QueryToken token) at Microsoft.OData.Core.UriParser.Parsers.FilterBinder.BindFilter(QueryToken filter) at Microsoft.OData.Core.UriParser.ODataQueryOptionParser.ParseFilterImplementation(String filter, ODataUriParserConfiguration configuration, IEdmType elementType, IEdmNavigationSource navigationSource) at Microsoft.OData.Core.UriParser.ODataQueryOptionParser.ParseFilter() at System.Web.OData.Query.FilterQueryOption.get_FilterClause() at System.Web.OData.Query.Validators.FilterQueryValidator.Validate(FilterQueryOption filterQueryOption, ODataValidationSettings settings) at System.Web.OData.Query.FilterQueryOption.Validate(ODataValidationSettings validationSettings) at System.Web.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings) at System.Web.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings) at System.Web.OData.EnableQueryAttribute.ValidateQuery(HttpRequestMessage request, ODataQueryOptions queryOptions) at System.Web.OData.EnableQueryAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor) at System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)"
}-
}-
}
我意识到可以通过查询“Books”实体来获取该信息...
http://myBooksDatabase/Books?$expand=Author&$filter=contains(Name,'Harry')
...但我在尝试引用嵌套属性时遇到了问题,无论我怎么做。上面的查询有效,并呈现整个作者实体,但如果我添加&$select=Author/Name
,我会得到以下响应:
{
error: {
code: ""
message: "The query specified in the URI is not valid. Found a path with multiple navigation properties or a bad complex property path in a select clause. Please reword your query such that each level of select or expand only contains either TypeSegments or Properties."
innererror: {
message: "Found a path with multiple navigation properties or a bad complex property path in a select clause. Please reword your query such that each level of select or expand only contains either TypeSegments or Properties."
type: "Microsoft.OData.Core.ODataException"
stacktrace: " at Microsoft.OData.Core.UriParser.Visitors.SelectPropertyVisitor.ProcessTokenAsPath(NonSystemToken tokenIn) at Microsoft.OData.Core.UriParser.Visitors.SelectPropertyVisitor.Visit(NonSystemToken tokenIn) at Microsoft.OData.Core.UriParser.Syntactic.NonSystemToken.Accept(IPathSegmentTokenVisitor visitor) at Microsoft.OData.Core.UriParser.Parsers.SelectBinder.Bind(SelectToken tokenIn) at Microsoft.OData.Core.UriParser.Parsers.SelectExpandBinder.Bind(ExpandToken tokenIn) at Microsoft.OData.Core.UriParser.Parsers.SelectExpandSemanticBinder.Bind(IEdmStructuredType elementType, IEdmNavigationSource navigationSource, ExpandToken expandToken, SelectToken selectToken, ODataUriParserConfiguration configuration) at Microsoft.OData.Core.UriParser.ODataQueryOptionParser.ParseSelectAndExpandImplementation(String select, String expand, ODataUriParserConfiguration configuration, IEdmStructuredType elementType, IEdmNavigationSource navigationSource) at Microsoft.OData.Core.UriParser.ODataQueryOptionParser.ParseSelectAndExpand() at System.Web.OData.Query.Validators.SelectExpandQueryValidator.Validate(SelectExpandQueryOption selectExpandQueryOption, ODataValidationSettings validationSettings) at System.Web.OData.Query.SelectExpandQueryOption.Validate(ODataValidationSettings validationSettings) at System.Web.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings) at System.Web.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings) at System.Web.OData.EnableQueryAttribute.ValidateQuery(HttpRequestMessage request, ODataQueryOptions queryOptions) at System.Web.OData.EnableQueryAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor) at System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)"
}-
}-
}
这里是我的两个 OData 控制器,一个用于作者,另一个用于书籍:
namespace My.OData.Controllers
{
public class AuthorsController : ODataController
{
// GET /Author
[EnableQuery]
public IQueryable<Author> Get()
{
return MediaContext.Singleton.Authors;
}
// GET /Authors(<key>)
[EnableQuery]
public SingleResult<Author> Get([FromODataUri] Guid key)
{
var result = MediaContext.Singleton.Authors.Where(b => b.Id == key);
return SingleResult.Create(result);
}
// GET /Authors(<key>)/Books
[EnableQuery]
public IQueryable<Book> GetBooks([FromODataUri] Guid key)
{
return MediaContext.Singleton.Authors.Where(a => a.Id == key).SelectMany(author => author.Books);
}
}
public class BooksController : ODataController
{
// GET /Books
[EnableQuery]
public IQueryable<Book> Get()
{
return MediaContext.Singleton.Books;
}
// GET /Books(<key>)
[EnableQuery]
public SingleResult<Book> Get([FromODataUri] Guid key)
{
var result = MediaContext.Singleton.Books.Where(b => b.Id == key);
return SingleResult.Create(result);
}
// GET /Books(<key>)/Author
[EnableQuery]
public SingleResult<Author> GetAuthor([FromODataUri] Guid key)
{
return SingleResult.Create(MediaContext.Singleton.Books.Where(b => b.Id == key).Select(b => b.Author));
}
}
}
所以,就像我所说的那样,还有其他需要添加或配置的内容来使关联实体中的引用属性工作吗?