Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework LINQ to实体无法识别该方法_Entity Framework_Entity Framework 4_Code First_Ef Code First_Entity Framework Ctp5 - Fatal编程技术网

Entity framework LINQ to实体无法识别该方法

Entity framework LINQ to实体无法识别该方法,entity-framework,entity-framework-4,code-first,ef-code-first,entity-framework-ctp5,Entity Framework,Entity Framework 4,Code First,Ef Code First,Entity Framework Ctp5,我正在关注MSDN。我先把它移植到EF代码 public interface IUnitOfWork { IRepository<Employee> Employees { get; } IRepository<TimeCard> TimeCards { get; } void Commit(); } 这个模型很好,因为它给了我可测试性。但是,运行像上面这样的查询会抛出 LINQ to Entities does not recognize th

我正在关注MSDN。我先把它移植到EF代码

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.

我理解这个错误,但有没有办法避免它,但仍然保留存储库以供测试?

由于
IQueryable
和查询提供程序工作的性质,无法翻译select语句:有关详细信息,请参阅此线程

您可以通过将表达式划分为以下单独语句来“帮助”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
而不是
IQueryable
,这样您的原始表达式就可以工作了。

谢谢,它可以工作了。但IEnumerable是贪婪的。它将所有内容都带到内存中,并在之后进行连接。
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();
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.
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;