2016-10-11 10 views
0

내가이이 저장소 코드를 기반으로 다음과 같은 확장 방법DbSet는

/// <summary> 
    /// Provides a statically typed interface for db includes 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    /// <param name="set">The set.</param> 
    /// <param name="includes">The includes.</param> 
    /// <returns>DbSet&lt;T&gt;.</returns> 
    public static DbSet<T> Include<T>(this DbSet<T> set, params Expression<Func<T, object>>[] includes) where T : class 
    { 
     if (includes != null) 
     { 
      foreach (var expression in includes) 
      { 
       set.Include(expression); 
      } 
     } 
     return set; 
    } 

여기

그러나

https://github.com/carbonrobot/FullStackEF/tree/master/src

발견을 추가 한 루프 확장 방법을 포함 나는 이것을 활용하는 경우 다음

public ServiceResponse<Town> Get(int id) 
    { 
     Func<Patient> func = delegate 
     { 
      using (var context = _contextFactory()) 
      { 
       return context.Get<Town>(id, x => x.County); 
      } 
     }; 
     return this.Execute(func); 
    } 

마을 클래스에는 카운티 en tity.

베이스가 아닌 확장 메서드를 호출 할 때 무한 루프가 발생합니까?

내가 여기서 잘못하고있는 아이디어가 있습니까?

답변

1

그 방법에는 몇 가지 실수가 있습니다.

DbExtensions.Include 방법은 다음과 같은 서명이있다 : 당신이 볼 수 있듯이

public static IQueryable<T> Include<T, TProperty>(
    this IQueryable<T> source, 
    Expression<Func<T, TProperty>> path 
) 
where T : class 

, 그것은 IQueryable<T>를 수신하고있는 다른 IQueryable<T> 변수에 할당하고 위해 원본 대신 반환 효과를 위해 할을 반환 이 코드는 코드가 수행하지 않습니다. 이 방법은 IQueryable<T>보다 더 구체적이고 유형 DbSet<T>set 변수에 Include를 호출, 인수가 사용자 정의 메소드의 서명과 일치하기 때문에

또한, 컴파일러는 단순히 따라서 StackOverflowException 같은 메소드를 호출합니다. 자세한 explaination에 대한

public static IQueryable<T> Include<T>(this DbSet<T> set, params Expression<Func<T, object>>[] includes) 
    where T : class 
{ 
    var result = set.AsQueryable(); 
    if (includes != null) 
    { 
     foreach (var expression in includes) 
     { 
      result = result.Include(expression); 
     } 
    } 
    return result; 
} 
+0

감사 : 그와

여기에 올바른 사용자 정의 메소드 서명 및 구현이 말했다된다. Repo가 DbSet을 사용하여 메서드를 체인으로 반환 할 때 한 가지 질문이 있습니다. .include (includes) .find (id) 확장 메서드가 return IQueryable이기 때문에 (DbSet 과 연계 할 수있는 능력이 없어졌다. 어떤 아이디어라도? –

+0

흠, 해결할 수없는 것처럼 들리지만'Include'는 ' 어쨌든'Find '로 작업하기 때문에'Find'로 체인으로 연결하는 것은 의미가 없다고 생각합니다. 정규 LINQ 체인이 작동해야합니다.'Include'의 결과는'DbSet'이 아닙니다. –

+0

Ok 좋아, 기본적으로 .include (includes) .select (x => x.Id == id); (find 예제와 관련하여) 쿼리 할 수있는 결과를 필터링하려면? –