2012-07-18 2 views
2

DbContext 생성기를 통해 엔티티를 생성하고이를 엔티티 컨텍스트 모델을 사용하는 API 컨트롤러에 추가했습니다. 다음 방법은하지만 컴파일에 실패 :Entity Framework Include가 컴파일되지 않습니다.

public IEnumerable<casino> Getcasinos() 
    { 
     var casinos = db.casinos.Include(c => c.city).Include(c => c.state); 
     return casinos.AsEnumerable(); 
    } 

컴파일러는 말한다 : 왜 이런 말을한다

Cannot Convert Lambda Expression to Type 'String' Because It Is Not A Delegate Type 

어떤 아이디어? System.Linq 네임 스페이스를 가져 왔습니다.

답변

8

이것은 실제로 ObjectQuery(T).Include 메서드 때문에 발생합니다. 당신은 당신이 전화하는거야 곳에는 System.Data.Entity 네임 스페이스를 사용할 수 없기 때문에 그것은 아마보고있는 이유

public ObjectQuery<T> Include(string path); 

이유 :이 함수의 서명이 있습니다. DbExtensions 메타 데이터에서 당신은 식을 사용하여 IncludeSystem.Data.Entity 네임 스페이스를 필요로 볼 수 있습니다 :

당신이 System.Data.Entity 네임 스페이스를 포함하는 경우
namespace System.Data.Entity 
{ 
    [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", Justification = "Casing is intentional")] 
    public static class DbExtensions 
    { 
     public static IQueryable<T> Include<T, TProperty>(this IQueryable<T> source, Expression<Func<T, TProperty>> path) where T : class; 
     public static IQueryable<T> Include<T>(this IQueryable<T> source, string path) where T : class; 
     public static IQueryable Include(this IQueryable source, string path); 
    } 
} 

, 오류가 해결됩니다.

+0

잘 했어! 감사! –

+0

@JuniperAsh : 문제 없습니다. 이것은 실제로 데이터 액세스를 별도의 레이어에 마무리하는 경우 매우 쉽게 실수합니다. –

관련 문제