Search code examples
c#entity-frameworkweb-servicesasp.net-web-apiodata

Microsoft.OData.Client not loading related entities when calling a function with expand


I've created a Web API OData v4 service that I'm trying to consume using the Microsoft.OData.Client library OData Client T4 Template (ver. 2.3.0). The only thing I can't get to work is using the Expand method chaining to a bound function. It returns the primary entity, but it's not serializing the related entities I'm asking it to via the expand method. I've inspected the traffic using fiddler and the related entities are being returned from my service. And yes, I have updated all NuGet Libraries to the latest versions. I cannot find any information about this on the web. Any help is appreciated.

I've listed some of my code below for reference:

Client.cs:

// Doesn't work returning a collection
var collectionResult = this.LibraryFolders.ByKey(newLibrary.Id).RefreshLibrary().Expand("MediaFiles").First();

// And doesn't work returning a single result
var singleResult     = this.LibraryFolders.ByKey(newLibrary.Id).RefreshLibrary().Expand("MediaFiles").GetValue();

I don't think the issue is in my service code since it's working as expected, but showing it for reference...

LibraryFoldersController.cs:

[HttpGet]
[EnableQuery]
public SingleResult<LibraryFolder> RefreshLibrary([FromODataUri] long key)
{
    this.businessController.RefreshLibrary(key);

    return this.GetLibraryFolder(key);
}

WebApiConfig.cs:

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();

    config.MapODataServiceRoute("ODataRoute", "api", GetModel());
}

public static Microsoft.OData.Edm.IEdmModel GetModel()
{
    var builder = new ODataConventionModelBuilder
    {
        Namespace = "OData"
    };

    builder.EntitySet<LibraryFolder>("LibraryFolders");
    builder.EntitySet<MediaFile>("MediaFiles");

    builder.EntitySet<LibraryFolder>("LibraryFolders")
            .EntityType
            .Function("RefreshLibrary")
            .ReturnsFromEntitySet<LibraryFolder>("LibraryFolders");

    return builder.GetEdmModel();
}

Solution

  • It seems an OData client bug. I create an issue to track: https://github.com/OData/odata.net/issues/285 Thanks your information.