How to enable nested json result in OData (ASP.NET Core API)
-
I'm using the following controller to get data from database using EF Core:
public class EquipmentsController : ODataController
{
private readonly SqlServerContext _sqlServerContext;public EquipmentsController(SqlServerContext sqlServerContext) { \_sqlServerContext = sqlServerContext; } \[HttpGet\] \[EnableQuery\] public ActionResult\> Get() { IQueryable eqList = \_sqlServerContext.Equipments.AsQueryable() .Include(x => x.CostCenter) .Include(x => x.EquipmentCategory) .Include(x => x.equipmentType); return Ok(eqList); } }
My problem is that I cannot get nested result in the final json while I have included CostCenter, EquipmentCategory, and equipmentType entities. I'm using ASP.NET Core 6 API and OData 8.0.12 My ED model is as follows:
static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new();
builder.EntitySet("Equipments");
return builder.GetEdmModel();
}How can I fix that?
-
I'm using the following controller to get data from database using EF Core:
public class EquipmentsController : ODataController
{
private readonly SqlServerContext _sqlServerContext;public EquipmentsController(SqlServerContext sqlServerContext) { \_sqlServerContext = sqlServerContext; } \[HttpGet\] \[EnableQuery\] public ActionResult\> Get() { IQueryable eqList = \_sqlServerContext.Equipments.AsQueryable() .Include(x => x.CostCenter) .Include(x => x.EquipmentCategory) .Include(x => x.equipmentType); return Ok(eqList); } }
My problem is that I cannot get nested result in the final json while I have included CostCenter, EquipmentCategory, and equipmentType entities. I'm using ASP.NET Core 6 API and OData 8.0.12 My ED model is as follows:
static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new();
builder.EntitySet("Equipments");
return builder.GetEdmModel();
}How can I fix that?
Have you included the relevant
$expand
options in your query? OData Expand - OData | Microsoft Learn[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Have you included the relevant
$expand
options in your query? OData Expand - OData | Microsoft Learn[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks. Solved.