2016-11-01 3 views
1

내 도메인 모델은 다음과 같습니다엔티티 프레임 워크 코어를 사용하여 모든 레벨을로드하는 방법

예약

public class Book 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public int ReleaseYear { get; set; } 

    public virtual ICollection<Store> Stores { get; set; } 
} 

스토어

public class Store 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public int BookId { get; set; } 

    public virtual Book Book { get; set; } 
    public virtual ICollection<StoreLocation> Locations { get; set; } 
} 

StoreLocation

,536,
public class StoreLocation 
{ 
    public int Id { get; set; } 
    public string Address { get; set; } 
    public int StoreId { get; set; } 

    public virtual Store Store { get; set; } 
} 

Book의 모든 레벨 (및 하위 레벨)을 어떻게 포함 할 수 있습니까?

public Book GetBookById(int Id) 
{ 
    return this._DbContext.Books 
     .Include(p => p.Stores) 
     .FirstOrDefault(p => p.Id == Id); 
} 

나는 무엇을 시도 했습니까?

  • .ThenInclude(p => p.StoreLocation)이 작동하지 않습니다.
+1

[ThenInclude] (https://docs.efproject.net/en/latest/querying/related-data.html#including-multiple-levels)를 사용해 보셨습니까? – tmg

+0

예. 다른 List에'List'가있을 때는 두 번째 것을 포함 할 수 없습니다. –

답변

1

아래와 같이해야합니다.

return this._DbContext.Books.Include(p => p.Stores).ThenInclude(q => q.Locations) 
        .FirstOrDefault(p => p.Id == Id); 

주 : 즉에 대한 D 명중 한명 솔루션이 될 수있는 VS을 닫고 그렇지 않은 계세요하지만 새로운 인스턴스를 다시 시작합니다 때때로 VS는 인텔리 조심 properly.So intelisence을 표시하지 않습니다 일하면서, 당신이 원하는 것을 입력하십시오. 그 후에 컴파일되고 제대로 작동합니다.

+0

작동하지 않습니다. '.ThenInclude (q => q ... ')에 나와있는 목록을위한 메소 (methos)를 보여줍니다. 여러분의 모델에 따라 –

+0

첫 번째 것으로 가정합니다. 목록 번호가 아니어야합니까? 그 다음 문제가 무엇입니까? – Sampath

+0

아니요, 작동하지 않습니다. 예 목록에 다른 목록이 포함되어 있습니다. 두 번째 (위치)를 포함 할 수 없습니다. '.ThenInclude'를 사용할 때'Location' poperty가 없습니다. 대신에 일반적인 방법이 있습니다. –