I am trying to perform a nested expand on one of my odata queries (3 levels) and am unable to get the expanded object to be returned.
Here are my classes:
public class Project
{
public int ProjectId { get; set; }
public Manager Manager { get; set; }
public IEnumerable<ProjectResource> ProjectResources { get; set; }
}
public class ProjectResource
{
public int ProjectResourceId { get; set; }
public Employee Employee { get; set; }
public Project Project { get; set; }
public IEnumerable<Forecast> Forecasts { get; set; }
}
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public Manager Manager { get; set; }
public IEnumerable<ProjectResource> ProjectResources { get; set; }
}
public class Manager
{
public int ManagerId { get; set; }
public string Name { get; set; }
}
public class Forecast
{
public int ForecastId { get; set; }
public ProjectResource ProjectResource { get; set; }
}
The following odata query works, and a list of Projects and their Managers is returned:
/Projects?$expand=Manager
I then tried this query, and it worked:
/Forecasts?$expand=ProjectResource($expand=Project)
Adding another level of expand:
/Forecasts?$expand=ProjectResource($expand=Project($expand=Manager))
And I get an error saying that I can only expand 2 levels. So I add this to my query and retry:
[EnableQuery(MaxExpansionDepth=0)]
I don't get the error anymore, but the Manager for each Project is not included. Any idea on what I'm missing?
I was struggling with the same problem. Turns out that adding all of the entity sets during the configuration allows the $expand to work properly after 2 levels.
Add all your entity sets like this:
builder.EntitySet<Item>("Items");
builder.EntitySet<Product>("Products");
builder.EntitySet<Model>("Models");
builder.EntitySet<Type>("Types");
I also set [EnableQuery(MaxExpansionDepth=0)] as an attribute on the controller.
You need to add all of the entity sets during the configuration allows the $expand to work properly after 2 levels.
The SO Question that claims that this issue is related to complex type is NOT valid.
Hope it helps save someone his time, just wasted 2 days because of this!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With