簡體   English   中英

包括實體框架核心(數據循環)

[英]Include Entity Framework Core (Loop of data)

我想在工作站列表中包含已連接的設備。 但我不僅得到了設備。 我也得到了工作站。 這是一種循環,沒有必要。 我怎樣才能停止再次包括工作站,因為這也包括各種其他列表?

//returns to much
var workstations = this.context.TWorkstations 
   .Include(x => x.TDevices)
   .AsQueryable();

//crash -> see error msg
var workstations = this.context.TWorkstations 
   .Include(x => x.TDevices).ThenInclude(d => d.Select(y => y.Alias))
   .AsQueryable();

//crash -> see error msg
var workstations = this.context.TWorkstations 
   .Include(x => x.TDevices).ThenInclude(d => d.Alias))
   .AsQueryable();

錯誤:

表達式“d.Alias”在“包含”操作中無效,因為它不代表屬性訪問:“t => t.MyProperty”。 要定位在派生類型上聲明的導航,請使用強制轉換 ('t => ((Derived)t).MyProperty') 或 'as' 運算符 ('t => (t as Derived).MyProperty')。 集合導航訪問可以通過組合 Where、OrderBy(Descending)、ThenBy(Descending)、Skip 或 Take 操作進行過濾。 有關包含相關數據的詳細信息,請參閱http://go.microsoft.com/fwlink/?LinkID=746393

[
{
    "id": 102,
    "workstation": "workstationName",
    "comments": [],
    "devices": [
        {
            "id": 93524,
            "alias": "xxx",
            "workstation": {
                "id": 102,
                "workstation": "workstationName",

Include旨在返回實體的所有數據,它還會自動初始化所有已加載的相關屬性。 所以只需使用完全自定義的投影。

var workstations = 
   from w this.context.TWorkstations 
   select new 
   {
      w.Id,
      workstation = w.workstationName
      devices = w.TDevices.Select(d => d.Alias).ToArray()
   };

暫無
暫無

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

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