2012-02-17 3 views
0

머리카락이 빠지기 때문에 AdPhotos 및 Location 엔티티를 반환 할 수없는 이유가 없습니다. .ToList()를 사용하여 AdPhotos 컬렉션을 손상시키지 않아야합니까?EF에서 열혈적로드가 작동하지 않습니다.

반환 지점에 중단 점을 배치하면 AdPhotos 및 위치에서 데이터를 볼 수 있지만 그 후에는 사라집니다.

public List<AdListing> LatestAdListings() 
{ 
    using (var db = new AdultdirectoryEntities()) 
    { 
     var results = (from a in db.AdListings.Include("AdPhotos").Include("Location") 
         join l in db.Locations on a.LocationID equals l.LocationID 
         where a.Approved && l.CountryID == Constants.ItemKeys.UsCountryId && a.AdPhotos.Count > 0 
         orderby a.CreateDateTime descending 
         select a).Take(5).ToList(); 

     return results; 
    } 
} 
+0

을 (http://msdn.microsoft.com/en-us/library/bb896272.aspx) "당신이 Include를 호출 할 때 쿼리 경로는 ObjectQuery의 반환 된 인스턴스에서만 유효합니다 .ObjectQuery의 다른 인스턴스와 개체 컨텍스트 자체는 영향을받지 않습니다." 이것이 제 문제라고 생각합니다. 누구든지 완전한 관계를 회복하기위한 더 나은 해결책을 가지고 있습니까? – TheWebGuy

답변

1

Include 전화는 수동 가입이 뒤 따르며 지원되지 않습니다. 수동 조인 또는 투영을 사용하면 쿼리 모양이 변경되고 모든 Include 호출이 손실됩니다. 또한

당신은 당신이 같은 쿼리를 쓸 수 있기 때문에 가입하지 않아도 :이 페이지에서

var results = (from a in db.AdListings.Include("AdPhotos").Include("Location") 
       where a.Approved && a.Location.CountryID == Constants.ItemKeys.UsCountryId && a.AdPhotos.Count > 0 
       orderby a.CreateDateTime descending 
       select a).Take(5).ToList(); 
+0

감사합니다! 이것은 도움이되었습니다. 나는 한 가지 더 질문이있다. id가 "위치"관계 내에서 다른 관계를 원한다면 어떻게해야합니까? 어떻게 수행합니까? – TheWebGuy

관련 문제