实体框架和存储库模式(问题的IQueryable) [英] Entity Framework and Repository Pattern (problem with IQueryable)

查看:161
本文介绍了实体框架和存储库模式(问题的IQueryable)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从LINQ的2 SQL切换到实体框架,而我看到一些奇怪的行为,在EF那我希​​望有人可以帮助。我试着谷歌搜索左右,但我没能找到其他人与此相同的问题。我嘲笑了一个场景来说明情况。

I just switched from Linq 2 SQL to Entity Framework, and I'm seeing some strange behaviors in EF that I'm hoping someone can help with. I tried Googling around, but I wasn't able to find other people with this same problem. I've mocked up a scenario to explain the situation.

如果我直接与EF环境中工作,我可以做一个选择中的选择。例如,该执行完美的罚款:

If I work directly with an EF context, I'm able to do a select within a select. For example, this executes perfectly fine:

        // this is an Entity Framework context that inherits from ObjectContext
        var dc = new MyContext();

        var companies1 = (from c in dc.Companies
                          select new {
                              Company = c,
                              UserCount = (from u in dc.CompanyUsers
                                           where u.CompanyId == c.Id
                                           select u).Count()
                          }).ToList();

不过,如果我使用存储库模式中,仓库将返回IQueryable的(甚至是对象集或的ObjectQuery),我得到一个NotSupportedException异常(LINQ到实体不能识别方法System.Linq.IQueryable`1)...

However, if I use a repository pattern where the repository is returning IQueryable (or even ObjectSet or ObjectQuery), I get a NotSupportedException (LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1)...

下面是我的仓库的一个例子:

Here is an example of my repository:

public class Repository {
    private MyContext _dc;

    public Repository() {
        _dc = new MyContext();
    }

    public IQueryable<Company> GetCompanies() {
        return _dc.Companies;
    }

    public IQueryable<CompanyUser> GetCompanyUsers() {
        return _dc.CompanyUsers;
    }
}

//我在使用其他类中的存储库(例如,在我服务层)

// I'm using the repository inside another class (e.g. in my Services layer)

        var repository = new Repository();

        var companies2 = (from c in repository.GetCompanies()
                          select new {
                              Company = c,
                              UserCount = (from u in repository.GetCompanyUsers()
                                           where u.CompanyId == c.Id
                                           select u).Count()
                          }).ToList();

以上code抛出一个NotSupportedException异常。

The above code throws a NotSupportedException.

我意识到,如果有公司和CompanyUsers之间的关联,那么我可以简单地做到这一点,它会正常工作:

I realize that if there's an association between Companies and CompanyUsers, then I can simply do this and it will work fine:

        var companies3 = (from c in repository.GetCompanies()
                          select new {
                              Company = c,
                              UserCount = (from u in c.CompanyUsers
                                           select u).Count()
                          }).ToList();

...但我的例子中是一个比较复杂的场景只是一个简化版本,我没有实体之间的关联。

...but my example is just a simplified version of a more complicated scenario where I don't have an association between the entities.

所以我很困惑,为什么实体框架是抛出了NotSupportedException异常。它是如何查询工作完全正常,当我与EF背景下直接工作,但它不支持,如果我正在使用的IQueryable从另外一个方法返回。这工作完全正常使用LINQ 2 SQL,但它似乎并没有在实体框架的工作。

So I'm very confused why Entity Framework is throwing the NotSupportedException. How is it that the query works perfectly fine when I'm working with the EF context directly, but it's not supported if I'm working with IQueryable returned from another method. This worked perfectly fine with Linq 2 SQL, but it doesn't seem to work in Entity Framework.

任何有识之士将大大AP preciated。

Any insight would be greatly appreciated.

在此先感谢。

推荐答案

我怀疑发生的事情是,EF看到前pression为 repository.GetCompanyUsers()拉姆达为里面的第选择,不知道该怎么办,因为不是EF背景。我认为,如果你传入IQueryable的,而不是直接的当然pression返回它,它应该工作。

I suspect that what's happening is that EF sees the expression for repository.GetCompanyUsers() inside the lambda for the first select and doesn't know what to do with it because repository isn't an EF context. I think that if you pass in the IQueryable directly instead of an expression that returns it, it should work.

怎么样,如果你这样做:

How about if you do this:

    var companyUsers = repository.GetCompanyUsers();
    var companies2 = (from c in repository.GetCompanies() 
                      select new { 
                          Company = c, 
                          UserCount = (from u in companyUsers 
                                       where u.CompanyId == c.Id 
                                       select u).Count() 
                      }).ToList(); 

这篇关于实体框架和存储库模式(问题的IQueryable)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆