OData $ expand client not working

Software used:

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

Consider the following model:

Location (Id, LocationName, Street, PostalCodeId)

PostalCode (Id, ZIP)

A location has one PostalCode and PostalCode has many locations.

This is OData-Configuration:

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 called 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 make the same request in the application, it doesn't 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 running this code, PostalCode

there is null

.

+3


source to share


1 answer


Martinaut

Is there any query operation between new Container...

and var result= ...

?

If so, add the following codes:



container.MergeOption = MergeOption.OverwriteChanges;

      

Thank.

+6


source







All Articles