I am trying to get data from five tables: Category - subCategory - secondSubCategory - type - heating which are all related to main table (property).
Tables (category - subCategory - secondSubCategory) are related like layers (Category => subCategory => secondSubCategory)
I tried to get data with:
public IActionResult getAllProperties()
{
var properties = db.properties
.Include(cat => cat.category)
.Include(sub => sub.subCategory)
.Include(sec => sec.SecondSubCategory)
.Include(e => e.heating)
.Include(e => e.type)
.OrderByDescending(x => x.id)
.ToList();
return Ok(properties);
}
but the returned data was with values for type and heating fields but with null values for (categoryId and subCategoryId and secondSubCategoryId) knowing that those fields have values
Property.cs
public class Property
{
[Key]
public int id { get; set; }
public int typeId { get; set; }
public type type { get; set; }
public int heatingId { get; set; }
public heating heating { get; set; }
public int? categoryId { get; set; }
public category category { get; set; }
public int? subCategoryId { get; set; }
public subCategory subCategory { get; set; }
public int? secondSubCategoryId { get; set; }
public SecondSubCategory SecondSubCategory { get; set; }
}
Response without including category and subCategory and secondSubCategory :
{
"id": 14,
"typeId": 1,
"type": {
"id": 1,
"typeName": "Flat"
},
"heatingId": 4,
"heating": {
"id": 4,
"heatingName": "Conditioning"
},
"categoryId": 1,
"category": null,
"subCategoryId": 2,
"subCategory": null,
"secondSubCategoryId": 3,
"secondSubCategory": null
}
Response with including category and subCategory and secondSubCategory :
{
"id": 14,
"typeId": 1,
"type": {
"id": 1,
"typeName": "Flat"
},
"heatingId": 4,
"heating": {
"id": 4,
"heatingName": "Conditioning"
},
"categoryId": null,
"category": null,
"subCategoryId": null,
"subCategory": null,
"secondSubCategoryId": null,
"secondSubCategory": null
}
Category.cs
public class category
{
public int id { get; set; }
public string category_Name { get; set; }
public IList<subCategory> subCategories { get; set; }
public Property Property { get; set; }
}
subCategory.cs:
public class subCategory
{
public int id { get; set; }
public string subCategoryName { get; set; }
public int CategoryId { get; set; }
public category category { get; set; }
public IList<SecondSubCategory> secondSubCategories { get; set; }
public Property Property { get; set; }
}
secondSubCategory.cs:
public class SecondSubCategory
{
public int id { get; set; }
public string subCategoryName { get; set; }
public int subCategoryId { get; set; }
public subCategory subCategory { get; set; }
public Property Property { get; set; }
}
.Include(cat=>cat.category)in the query?.Include(s=>s.category)for that.public class category { public int id { get; set; } public string category_Name { get; set; } public IList<subCategory> subCategories { get; set; } public Property Property { get; set; } }