Assume we have two entities, Parent
and Child
, and Child
is a navigation property of Parent
(1:1 for the sake of this example). A particular parent's child can be fetched using this request: http://example.com/Parent(1)/Child
. To make that work, a GetChild()
method must be provided in the ParentController
to fetch the correct Child
entity.
Microsoft.OData.Client
has a LoadProperty
method that lets you fetch navigation properties on demand like this:
var parent = container.Parents.ByKey(1).GetValue();
container.LoadProperty(parent, "Child");
But since LoadProperty
internally creates a http://example.com/Parent(1)/Child
request, the call fails if I didn't write a GetChild()
method:
No routing convention was found to select an action for the OData path with template '~/entityset/key/navigation'.
In a project with many navigation properties it can take a lot of work to write all the getter methods. Is there a way to load navigation properties without having to write getters for every single relation?
Edit: I should have made clear that I'm aware of $expand
--but sometimes it's desired to fetch related entities long after the initial request (that's what LoadProperty() does, and what requires a getter for every single relation).
OData supports inline expansion of related entities. You can solve this problem on the client side by using the Expand<T>
method on the object returned by ByKey
as follows:
var parent = container.Parents.ByKey(1).Expand(p => p.Child).GetValue();
This call will result in an HTTP request similar to:
GET http://example.com/Parent(1)?$expand=Child
You can also chain calls to Expand<T>
.