6

MSDN에서 this article을 따르고 있습니다. EF Code First로 포팅했습니다. LINQ to Entities가 메서드를 인식하지 못합니다.

public class SqlUnitOfWork : IUnitOfWork, IDisposable 
{ 
    private readonly HrContext context; 
    private IRepository<Employee> employees; 
    private IRepository<TimeCard> timeCards; 
    public SqlUnitOfWork() 
    { 
     this.context = new HrContext(); 
    } 
    public IRepository<Employee> Employees 
    { 
     get 
     { 
      return new SqlRepository<Employee>(context); 
     } 
    } 
    public IRepository<TimeCard> TimeCards 
    { 
     get 
     { 
      return new SqlRepository<TimeCard>(context); 
     } 
    } 
    public void Commit() 
    { 
     this.context.SaveChanges(); 
    } 
    public void Dispose() 
    { 
     context.Dispose(); 
    } 
} 

var query = from e in unitOfWork.Employees.FindAll() 
      from tc in unitOfWork.TimeCards.FindAll() 
      where tc.Employee.Id == e.Id && e.Name.StartsWith("C") 
      select tc; 
var timeCards = query.ToList(); 

이 나에게 테스트 용이성을주기 때문에이 모델은 중대하다

public class SqlRepository<T> : IRepository<T> 
    where T : class 
{ 
    private readonly DbSet<T> entitySet; 
    public SqlRepository(DbContext context) 
    { 
     this.entitySet = context.Set<T>(); 
    } 
    public void Add(T newEntity) 
    { 
     this.entitySet.Add(newEntity); 
    } 
    public IQueryable<T> FindAll() 
    { 
     return this.entitySet; 
    } 
    public T FindById(params object[] keys) 
    { 
     return this.entitySet.Find(keys); 
    } 
    public IQueryable<T> FindWhere(Expression<Func<T, bool>> predicate) 
    { 
     return this.entitySet.Where(predicate); 
    } 
    public void Remove(T entity) 
    { 
     this.entitySet.Remove(entity); 
    } 
} 



public class HrContext : DbContext 
{ 
    public DbSet<Employee> Employees { get; set; } 
    public DbSet<TimeCard> TimeCards { get; set; } 
    protected override void OnModelCreating(ModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Employee>() 
        .HasMany(e => e.TimeCards) 
        .WithOptional(tc => tc.Employee); 
    } 
} 

public interface IUnitOfWork 
{ 
    IRepository<Employee> Employees { get; } 
    IRepository<TimeCard> TimeCards { get; } 
    void Commit(); 
} 
. 하나는 위의이

LINQ to Entities does not recognize the method 
'System.Linq.IQueryable`1[DomainModel.Models.TimeCard] FindAll()' 
    method, and this method cannot be translated into a store expression. 

내가 오류를 이해 발생하지만, 그것을 피할 여전히 시험 가능성에 대한 저장소를 유지하기 위해 어떤 방법이 같은 그러나 쿼리를 실행?

답변

4

당신의 선택 문 인해 IQueryable<T> 쿼리 공급자 작업 방법의 특성으로 변환 될 수없는 자세한 정보를 원하시면 What is the difference between IQueryable<T> and IEnumerable<T>?

이 스레드를 참조 할 수 있습니다 '도움'와 같은 별도의 문장으로 표현을 나누어 LINQ 공급자 이 :

var ems = unitOfWork.Employees.FindAll(); 
var tcs = unitOfWork.TimeCards.FindAll(); 

var query = from e in ems 
      from tc in tcs 
      where tc.Employee.Id == e.Id && e.Name.StartsWith("C") 
      select tc; 

또는 당신은 findall은하도록 할 수 있습니다() IEnumerable<T> 대신 IQueryable<T> 리턴하여 원래의 표현은 작동합니다.

+2

감사합니다. IEnumerable 비록 욕심입니다. 그것은 메모리에 모든 것을 가져오고 이후에 참여합니다. –

관련 문제