2014-10-10 7 views
2

지금은 저장소에 외래 키를 포함하는 일반적인 메서드를 만들려고합니다. 내가 현재 가지고 무엇제네릭 확장 메서드를 만들 때 문제가 발생했습니다.

은 이것이다 :

public static class ExtensionMethods 
{ 
    private static IQueryable<T> IncludeProperties<T>(this DbSet<T> set, params Expression<Func<T, object>>[] includeProperties) 
    { 
     IQueryable<T> queryable = set; 
     foreach (var includeProperty in includeProperties) 
     { 
      queryable = queryable.Include(includeProperty); 
     } 

     return queryable; 
    } 
} 

그러나 나는 오류 얻을 컴파일 할 때 :

The type 'T' must be a reference type in order to use it as parameter 'TEntity' in the generic type or method 'System.Data.Entity.DbSet'

여기에 문제가 될 수 무엇을? 당신의 방법 서명의 끝

답변

7

추가] where T : class : 당신의 T 형식 인수가 TEntity과 호환되도록하기 위해서는 있도록

private static IQueryable<T> IncludeProperties<T>(
    this DbSet<T> set, 
    params Expression<Func<T, object>>[] includeProperties) 
    where T : class // <== add this constraint. 
{ 
    ... 
} 

DbSet<TEntity>이 제약 조건이, 같은 제약 조건이 있어야합니다.

+0

아하 이봐. 도와 주셔서 정말로 고맙습니다! – JensOlsen112

0

오류 메시지가 표시되면 DbSet의 제네릭 인수는 참조 형식이어야합니다. 일반적인 인수는 참조가 아닌 유형을 포함하여 무엇이든 될 수 있습니다. 형식을 참조하기 위해 제약해야합니다.

관련 문제