简体   繁体   English

OData客户端$ expand不起作用

[英]OData client $expand does not work

Software used: 使用的软件:

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

Consider the following Model: 考虑以下模型:

Location (Id, LocationName, Street, PostalCodeId) 位置(Id,LocationName,Street,PostalCodeId)

PostalCode (Id, ZIP) PostalCode(Id,ZIP)

A Location has one PostalCode and a PostalCode has many Locations . 位置有一个PostalCodePostalCode有很多位置

This is the OData-Configuration: 这是OData配置:

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

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

Model classes: 型号类:

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; }
}

When calling http://localhost:49938/odata/Locations?$expand=PostalCode&$orderby=LocationName in a browser $expand works: 在浏览器$ expand中调用http://localhost:49938/odata/Locations?$expand=PostalCode&$orderby=LocationName时:

{
  "@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();

When I execute this code PostalCode is null . 当我执行此代码时, PostalCodenull

Martinaut Martinaut

Is there any query operation between new Container... and var result= ... ? new Container...var result= ...之间是否有任何查询操作?

If yes, please add the following codes: 如果是,请添加以下代码:

container.MergeOption = MergeOption.OverwriteChanges;

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM