Odata查询不会扩展 [英] Odata query won't expand

查看:109
本文介绍了Odata查询不会扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下网址查询我的服务:

http://a.com:3080/odata/DiscussionVM(6)?$ expand = Section,用户

关于控制器方法:

[EnableQuery(MaxExpansionDepth = 7)]
        public SingleResult<DiscussionVM> GetDiscussionVM([FromODataUri] int key)
        {
            return SingleResult.Create(db.DiscussionVMs.Where(discussionVM => discussionVM.DiscussionId == key));
        }

这有效并返回正确的JSON.

但是我随后在其他模型上运行稍微高级一些的查询:

http://a.com:3080/odata/OrganisationVM(30)?& $ expand = Categories($ expand = Discussions($ expand = Section,User))

和控制器操作:

// GET: odata/OrganisationVM(5)
        [EnableQuery(MaxExpansionDepth = 5, AllowedQueryOptions = AllowedQueryOptions.All)]
        public SingleResult<OrganisationVM> Get([FromODataUri] int key)
        {
            return SingleResult.Create(db.OrganisationVMs.Where(organisationVM => organisationVM.OrganisationId == key));
        }

这将返回下面的DiscussionVM JSON:

{
@odata.type: "#Models.DiscussionVM",
DiscussionId: 6,
Section_SectionID: 1005,
User_Id: "4cecc52e-ac3a-4696-ac6c-175af2a6378a",
DateCreated: "2014-12-06T00:00:00Z",
OrgCat_OrganisationCategoryId: 1,
Text: "Dummy section",
Html: null,
IsUserCreated: true,
Organisation_OrganisationId: null,
Positives: null,
Negatives: null,
CommentCount: 1
}

但不包含UserSection对象.没有引发任何错误.在数据库中查询(分析)正确的对象,并返回包括用户和分区的数据.

解决方案

我发现oData需要在其Edm模型中引用扩展的实体. 如果没有,它将在第一个级别后停止扩展,这就是为什么进一步扩展将不起作用的原因.

只需将您的可扩展EntitySet添加到IEdmModel中的ODataConventionModelBuilder中(在MapODataServiceRoute的模型配置中):

var builder = new ODataConventionModelBuilder();
// ...
builder.EntitySet<Categories>("categories");
// ...

希望这会有所帮助.

I'm querying my service using a url like:

http://a.com:3080/odata/DiscussionVM(6)?$expand=Section,User

on controller method:

[EnableQuery(MaxExpansionDepth = 7)]
        public SingleResult<DiscussionVM> GetDiscussionVM([FromODataUri] int key)
        {
            return SingleResult.Create(db.DiscussionVMs.Where(discussionVM => discussionVM.DiscussionId == key));
        }

This works and returns the correct JSON.

However I then run the slightly more advanced query on a different model:

http://a.com:3080/odata/OrganisationVM(30)?&$expand=Categories($expand=Discussions($expand=Section,User))

and controller action:

// GET: odata/OrganisationVM(5)
        [EnableQuery(MaxExpansionDepth = 5, AllowedQueryOptions = AllowedQueryOptions.All)]
        public SingleResult<OrganisationVM> Get([FromODataUri] int key)
        {
            return SingleResult.Create(db.OrganisationVMs.Where(organisationVM => organisationVM.OrganisationId == key));
        }

this returns the below DiscussionVM JSON:

{
@odata.type: "#Models.DiscussionVM",
DiscussionId: 6,
Section_SectionID: 1005,
User_Id: "4cecc52e-ac3a-4696-ac6c-175af2a6378a",
DateCreated: "2014-12-06T00:00:00Z",
OrgCat_OrganisationCategoryId: 1,
Text: "Dummy section",
Html: null,
IsUserCreated: true,
Organisation_OrganisationId: null,
Positives: null,
Negatives: null,
CommentCount: 1
}

But contains no User or Section object. No error is thrown. The correct objects are queried (profiled) in the database and data including user and section are returned.

解决方案

I discovred that oData needs the expanded entities to be referenced in its Edm Model. if not it will stop expanding after the first level, that's why further expands will not work.

Just add your expandable EntitySet to the ODataConventionModelBuilder in your IEdmModel (in MapODataServiceRoute's model config) :

var builder = new ODataConventionModelBuilder();
// ...
builder.EntitySet<Categories>("categories");
// ...

Hope this helps.

这篇关于Odata查询不会扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆