簡體   English   中英

Entity Framework Core:使用包含從相關表中獲取數據

[英]Entity Framework Core : get data from related tables with include

我正在嘗試從五個表中獲取數據:Category - subCategory - secondSubCategory - type - heat 這些都與主表(屬性)相關。

表 (category - subCategory - secondSubCategory) 像層一樣相關 (Category => subCategory => secondSubCategory)

我試圖通過以下方式獲取數據:

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);
}

但返回的數據包含類型和加熱字段的值,但知道這些字段具有值的(categoryId 和 subCategoryId 和 secondSubCategoryId)的 null 值

屬性.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; }
}

不包括 category 和 subCategory 和 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
    }

響應包括類別和子類別和第二子類別:

{
        "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
}

類別.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; }
  }

我首先使用代碼使用您的模型生成數據庫,並使用一些數據進行測試。 如果我不包括 category 和 subCategory 和 secondSubCategory,結果和你的一樣,但是當我包括它們時,會有一個期望:

JsonException:檢測到不支持的可能的 object 循環。 這可能是由於循環或 object 深度大於最大允許深度 32 所致。

然后我使用 NewtonsoftJson 來處理 ReferenceLoopHandling 問題

services.AddControllersWithViews()
    .AddNewtonsoftJson(options =>
     options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

之后,我可以從查詢中獲取類別和 subCategory 和 secondSubCategory。

正如我從您的實體那里得到的那樣,您必須在數據庫設計中遵循層次結構原則。 這是我的建議:

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 class category
  {
    public int id { get; set; }
    public string category_Name { get; set; }

    public IList<subCategory> subCategories { get; set; }
    public Property Property { get; set; }

  }

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 class SecondSubCategory
  {
    public int id { get; set; }
    public string secondCategoryName { get; set; }

    public int subCategoryId { get; set; }
    public subCategory subCategory { get; set; }
  }

以下是檢索層次結構數據的方法:

public IActionResult getAllProperties()
{
      var properties = db.properties
                         .Include(cat => cat.category)
                             .ThenInclude(sub => sub.subCategory)
                                .ThenInclude(sec => sec.SecondSubCategory)
                         .Include(e => e.heating)
                         .Include(e => e.type)        
                         .OrderByDescending(x => x.id)
                         .ToList();
      return Ok(properties);
}

您不能直接將屬性類別、子類別和子子類別對象包括在內,因為其中一些對象僅在彼此內部。 所以試着用好的舊方法來做到這一點:

public IActionResult getAllProperties()
{
 var properties = ( from p in  db.properties
 join  c in  db.category on p.categoryId equals c.Id into cj
 from c in cj.DefaultIfEmpty()
 join  sc in  db.subCategory on on p.subCategoryId equals sc.Id into scj
 from sc in scj.DefaultIfEmpty()
 join  ssc in  db.secondSubCategory on on p.secondSubCategoryId equals ssc.Id into sscj
 from ssc in sscj.DefaultIfEmpty()
join  h in  db.heatings on p.heatingId equals h.Id 
join  t in  db.types on p.typeId equals t.Id 
orderby  p.id descending
select new Property {
id= p.id 
 typeId=p.typeId,
 type=t,
 heatingId = p.heatingId,
 heating=h, 
 categoryId = p.categoryId,
category =c,
subCategoryId= p.subCategoryId,
subCategory=sc,
secondSubCategoryId=p.secondSubCategoryId,
SecondSubCategory=ssc 
}).ToList();

 return Ok(properties);
}

或更短一點:

var properties = ( from p in  db.properties
 join  c in  db.category on p.categoryId equals c.Id into cj
 from c in cj.DefaultIfEmpty()
 join  sc in  db.subCategory on on p.subCategoryId equals sc.Id into scj
 from sc in scj.DefaultIfEmpty()
 join  ssc in  db.secondSubCategory on on p.secondSubCategoryId equals ssc.Id into sscj
 from ssc in sscj.DefaultIfEmpty()
orderby  p.id descending
select new Property {
id= p.id 
 typeId=p.typeId,
 type=p.type,
 heatingId = p.heatingId,
 heating=p.heading, 
 categoryId = p.categoryId,
category =c,
subCategoryId= p.subCategoryId,
subCategory=sc,
secondSubCategoryId=p.secondSubCategoryId,
SecondSubCategory=ssc 
}).ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM