Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

entity framework - LINQ to Entities does not recognize the method

I am following this article on MSDN. I ported it to EF Code First.

public interface IUnitOfWork
{
    IRepository<Employee> Employees { get; }
    IRepository<TimeCard> TimeCards { get; }
    void Commit();
}

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 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 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();

This model is great as it gives me testability. However, running queries like the one above throws this

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.

I understand the error but is there any way to avoid it but still keep the repositories for testability?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your select statement cannot be translated due to the nature of how IQueryable<T> and the query providers work: see this thread for more info What is the difference between IQueryable<T> and IEnumerable<T>?

You can 'help' the linq provider by dividing your expression into separate statements like this:

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;

Or you can let FindAll() return IEnumerable<T> instead of IQueryable<T> and then your original expression should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...