OData客户端$ expand不起作用 [英] OData client $expand does not work

查看:84
本文介绍了OData客户端$ expand不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的软件:

  • ASP.Net Web Api 2
  • OData v4
  • Microsoft OData Client 6.13

请考虑以下模型:

位置(标识,位置名称,街道,邮政编号)

Location (Id, LocationName, Street, PostalCodeId)

PostalCode(编号,邮政编码)

PostalCode (Id, ZIP)

一个位置有一个 PostalCode ,一个 PostalCode 具有许多 Locations .

A Location has one PostalCode and a PostalCode has many Locations.

这是OData配置:

ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Location>("Locations");
builder.EntitySet<PostalCode>("PostalCodes");

config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: "odata",
            model: builder.GetEdmModel());

模型类:

public class Location {
    [Key]
    public int Id { get; set; }
    public String LocationName { get; set; }
    public String Street { get; set; }
    public int PostalCodeId { get; set; }
    [ForeignKey("PostalCodeId")]
    public PostalCode PostalCode { get; set; }
}

public class PostalCode {
    public int Id { get; set; }
    public string ZIP { get; set; }
    public List<Location> Locations { get; set; }
}

在浏览器 $ expand 中调用 http://localhost:49938/odata/Locations?$ expand = PostalCode& $ orderby = LocationName 时,工作原理:

When calling http://localhost:49938/odata/Locations?$expand=PostalCode&$orderby=LocationName in a browser $expand works:

{
  "@odata.context": "http://localhost:49938/odata/$metadata#Locations",
  "value": [
    {
      "Id": 1,
      "LocationName": "My Location 1",
      "Street": "Street 7",
      "PostalCodeId": 1838,
      "PostalCode": {
        "Id": 1838,
        "ZIP": "4081"
      }
    }
  ]
}

但是当我在应用程序中执行相同的请求时,它不起作用:

But when I do the same request in the application, it does not work:

Container c = new Container(new Uri("http://localhost:49938/odata/"));
var result = c.Locations
                 .Expand(x => x.PostalCode)
                 .OrderBy(x => x.LocationName)
                 .ToList();

当我执行此代码时, PostalCode null .

When I execute this code PostalCode is null.

推荐答案

Martinaut

Martinaut

新容器... var结果= ... 之间是否有任何查询操作?

Is there any query operation between new Container... and var result= ...?

如果是,请添加以下代码:

If yes, please add the following codes:

container.MergeOption = MergeOption.OverwriteChanges;

谢谢.

这篇关于OData客户端$ expand不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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