I'm running an ASP.NET Core Web API project and I have this model class with a self-referencing association :
public class VehicleCategory
{
[Key]
public int Id { get; set; }
public string description { get; set; }
[ForeignKey("ParentVehicleCategoryId")]
public int? ParentVehicleCategoryId { get; set; }
[JsonIgnore]
public virtual VehicleCategory ParentVehicleCategory { get; set; }
public virtual IEnumerable<VehicleCategory> subVehicleCategories { get; set; }
}
I have configured everything in EF to retrieve get all data and added this config into the startup.cs
file to prevent looping:
services.AddControllers().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
The problem is that when I try to get all data from the database, I end up with this json result :
[
{
"id": 7,
"description": "description 1",
"parentVehicleCategoryId": null,
"subVehicleCategories": [
{
"id": 9,
"description": "description 2",
"parentVehicleCategoryId": 7,
"subVehicleCategories": null
},
{
"id": 10,
"description": "description 3",
"parentVehicleCategoryId": 7,
"subVehicleCategories": null
}
]
},
{
"id": 9,
"description": "description 2",
"parentVehicleCategoryId": 7,
"subVehicleCategories": null
},
{
"id": 10,
"description": "description 3",
"parentVehicleCategoryId": 7,
"subVehicleCategories": null
}
]
Is there a way I can only have this data with only the array representing the children:
[
{
"id": 7,
"description": "description 1",
"parentVehicleCategoryId": null,
"subVehicleCategories": [
{
"id": 9,
"description": "description 2",
"parentVehicleCategoryId": 7,
"subVehicleCategories": null
},
{
"id": 10,
"description": "description 3",
"parentVehicleCategoryId": 7,
"subVehicleCategories": null
}
]
},
]
To get data I'm using this code:
var test = _raceContext.VehicleCategories
.Include(x => x.subVehicleCategories)
.ToList();