2013-12-16 3 views

답변

4

EF에서 조인 테이블은 탐색 속성을 사용하여 수행됩니다. 기본적으로, EF는 당신을 위해 그것을합니다. 리포지토리를 구현할 때 Generic이든 아니든간에 쿼리 표현식을 만들 때 Include 메서드를 호출하여 EF에 탐색 속성을 채우라고 지시 할 수 있습니다.

의 우리가 이러한 POCO 클래스가 있다고 가정 해 봅시다 : 여기

public class Dog 
{ 
    public int DogId { get; set; } 
    public string Name { get; set; } 

    public int OwnerId { get; set;} 
    public Owner Owner { get; set; } // the navigation property 
} 

public class Owner 
{ 
    public int OwnerId { get; set; } 
    public string Name { get; set; } 

    // another navigation property 
    // all the dogs that are related or owned by this specific owner 
    public ICollection<Dog> DogList { get; set; } 
    public ICollection<Cat> CatList { get; set; } 
} 

은 포함 사용하여 샘플 코드입니다 :

public virtual IEnumerable<Dog> Retrieve() 
{ 
    var _query = context.Dog.Include(a => a.Owner); 
    ... 
    ...// rest of your code 
} 

을 그리고 여러 테이블을 위해 당신은 중첩 그래서 같은 방법을 포함 할 수 있습니다

public virtual IEnumerable<Owner> Retrieve() 
{ 
    // you can nest as many as you want if there are more nav properties 
    var _query = context.Owner 
     .Include(a => a.DogList) 
     .Include(a => a.CatList); 
    ... 
    ...// rest of your code 
} 

일단 nav 속성을 포함하면 기본적으로 다른 테이블에 합류하게됩니다. 쿼리에서 생성되는 SQL을 살펴보십시오. 희망이 도움이!

+0

고맙습니다 ... 매우 도움이됩니다. – user972255