OData – Create Entity with Child Entities Response with Expand.

The Issue

When creating an entity (with child entities) using POST with OData the response will only show the main Entity created. Child entities are ignored.

The Solution

Use Expand on POST. What?! Seems a bit strange I’m sure but decorating your POST action with [EnableQuery] and wrapping the response in an OData SingleResult will adhear to the OData Expand on POST.

return Ok(SingleResult.Create(result));

The only issue with this is that you have to hit the DB with an Includes to get the fully formed newly created Entity plus child objects. So the result variable above will be assigned similar to below…..

var result = dbContext.ParentEntity.Include(“NameOfChildEntity”).Where(p => p.Id == myDbParentEntity.Id);

Leave a Comment

Filed under Comps

Leave a Reply