OData的V4的Web API 2.2深层次的扩大不工作 [英] OData v4 Web API 2.2 deep level expand not working

查看:294
本文介绍了OData的V4的Web API 2.2深层次的扩大不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况

我试图扩大项目三个层次:

I'm trying to expand "Item" to three levels:

Item.Product.Model.Type

Item.Product.Model.Type

所以我把这种嵌套网址查询选项:

So I call this nested query options url:

的http:// XXX / API /项 $展开;产品($ =扩展模型($扩大=型))

http://xxx/api/Items?$expand=Product($expand=Model($expand=Type))

我得到的2最大深度已经达到,所以我建议MaxExpansionDepth属性设置为3,但后来,不返回类型属性的警告!这是由涵盖这太问题

I Get a warning that the max depth of 2 has been reached so I set the suggested MaxExpansionDepth attribute to 3. But then, the "Type" property is not returned! This is covered by this SO question

然后我看<一个href=\"http://docs.oasis-open.org/odata/odata/v4.0/errata02/os/complete/part2-url-conventions/odata-v4.0-errata02-os-part2-url-conventions-complete.html#_Toc406398162\"相对=nofollow>官方的OData V4标准,它说我应该在扩大选择使用斜线,像这样:

Then I look at the official OData V4 standard and it says I should use slashes in the expand option, like so:

的http:// XXX / API /项 $ =扩大产品/型号/类型

http://xxx/api/Items?$expand=Product/Model/Type

但是,这给了我一个错误说明问题:

But that gives me an error telling:

在URI中指定的查询无效。发现跨越多个导航属性​​的路径。请重新表述查询,这样每个扩展路径只包含类型细分和导航属性。

The query specified in the URI is not valid. Found a path traversing multiple navigation properties. Please rephrase the query such that each expand path contains only type segments and navigation properties.

此<一个href=\"http://stackoverflow.com/questions/25246865/proper-way-to-call-nested-expand-from-net-odata-4-client\">SO回答井盖但得到的答复是矛盾的官方文档OData的。这是什么意思,甚至反正。

Which this SO answer covers but the answer is contradictory to the official OData doc. What does that even mean anyway.

问题

什么是官方的,标准和使用$扩大与V4的OData和Web API深层次查询选项的工作方式2.2

What is the official, standard and working way of using the $expand query option for deep levels with OData v4 and Web API 2.2

推荐答案

在聊天Jerther工作后,我们将问题范围缩小到不​​被标记为包含导航扩展属性。其结果是,在OData的框架,消除他们,因为他们没有定义相应的实体集。更新模型具体申报遏制似乎已经解决了这个问题。

After working with Jerther in chat, we narrowed the problem down to the expanded properties not being marked as contained navigations. As a result, the OData framework was removing them since they did not have corresponding entity sets defined. Updating the model to specifically declare the containment appears to have solved the problem.

围堵在一对夫妇的方式来指定,这取决于正在使用什么型号的建设者。在该 ODataConventionModelBuilder 您可以添加 System.Web.OData.Builder.ContainedAttribute 中有关财产的情况下, ,而对于 ODataModelBuilder 您可以使用 ContainsMany&LT; T&GT; 方法EntityTypeConfiguration 实例包含类。

Containment can be specified in a couple of ways, depending on what model builder is being used. In the case of the ODataConventionModelBuilder you can add the System.Web.OData.Builder.ContainedAttribute to the property in question, while for the ODataModelBuilder you can use the ContainsMany<T> method on the EntityTypeConfiguration instance for the containing class.

此外,此刻,级联扩展将停止,而一个复杂类型包含实体类型。

Also, at the moment, a cascaded expand will stop where a complex type contains an Entity type.

更新:

定义所有类型的链EntitySet的工作。

Defining all types in the chain as EntitySet works.

builder.EntitySet<Item>("Items");
builder.EntitySet<Product>("Products");
builder.EntitySet<Model>("Models");
builder.EntitySet<Type>("Types");

这似乎定义他们为的EntityType 是不够的。

在这里看到: https://github.com/OData/WebApi/issues/226

原来的答案

我试过reproing你的情况,并不能。有没有可能是类型是不是在你的行动被设置?这里是我的小摄制

I tried reproing your situation and couldn't. Is it possible that "Types" isn't being set in your action? Here was my little repro

public class ItemsController : ODataController
{
    [HttpGet]
    [EnableQuery(MaxExpansionDepth = 10)]
    [ODataRoute("/Items")]
    public IHttpActionResult GetItems()
    {
        return this.Ok(CreateItem());
    }

    private Item CreateItem()
    {
        return new Item
        {
            Id = 1,
            Products = new Product[]
            {
                new Product
                {
                    Id = 2,
                    Models = new Model[]
                    {
                        new Model
                        {
                            Id = 3,
                            Types = new MyType[]
                            {
                                new MyType
                                {
                                    Id = 4,
                                },
                            },
                        },
                    },
                },
            },
        };
    }
}

当与 /项目名为$展开;产品($扩大=模型($扩大=类型))导致以下:

{
    "@odata.context": "http://localhost:9001/$metadata#Items/$entity",
    "Id": 1,
    "Products@odata.context": "http://localhost:9001/$metadata#Items(1)/Products",
    "Products": [{
        "Id": 2,
        "Models@odata.context": "http://localhost:9001/$metadata#Items(1)/Products(2)/Models",
        "Models": [{
            "Id": 3,
            "Types@odata.context": "http://localhost:9001/$metadata#Items(1)/Products(2)/Models(3)/Types",
            "Types": [{
                "Id": 4
            }]
        }]
    }]
}

这篇关于OData的V4的Web API 2.2深层次的扩大不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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