使用实体框架连接视图时使用.include() [英] Using .Include() when joining a view using Entity Framework

查看:106
本文介绍了使用实体框架连接视图时使用.include()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用索引视图,该视图是没有任何关系的实体(与我的表实体的关系会更好,但这似乎几乎不可能实现)。此视图包含4个FK,它们共同构成PK:

Im using an Indexed View which is an entity without any relations (relations to my table entities would be preferable but this seemed to be nearly impossible to achieve). This view consists of 4 FK's which together form the PK:

PortalID
CategoryID
BranchID
CompanyID

现在我加入该视图来选择一组这样的公司:

Now im joining this view to select a set of companies like this:

var companies = (from c in database.Companies.FindAll().Include("City")
                 join l in database.CompanyList.FindAll() on c.ID equals l.CompanyID
                 where l.PortalID == 9 && l.BranchID == 1597
                 select c).Take(10);

实体公司与表格Cities(CityID,City)相关联,我想包括并完美运行。但是,当我加入视图时,.include()会被完全忽略,从而导致查询没有加入城市表。

The entity Company has an association with the table Cities (CityID, City), which i want to Include and works perfectly. However when i join the view the .Include() is ignored completely and thus results in a query without a join to the cities table.

您可能已经得出结论,使用im存储库模式,FindAll()返回一个IQueryable,并且ive手动将Include扩展添加到IQueryable,如下所示:

As you might have concluded im using a repository pattern and FindAll() returns an IQueryable and ive manually added the Include extension to IQueryable like this:

public static IQueryable<T> Include<T>(this IQueryable<T> sequence, string path) {
    var objectQuery = sequence as ObjectQuery<T>;
    if (objectQuery != null)
        return objectQuery.Include(path);

    return sequence;
}

我已经彻底测试了此扩展名并按应有的方式工作。

Ive thoroughly tested this extension and works like it should.

现在,对于我的问题,为什么它忽略了最终表达式树中的Include操作,又如何防止这样做呢?




更新:

我从以下链接获得了解决方案:
http://blogs.msdn.com/b/alexj/archive/2009/06/02/tip-22-how-to-make-include-really-include.aspx

So now for my question, why is it ignoring the Include operation in the final expression tree and how can i prevent it from doing so?

UPDATE:
I got my solution from the following link: http://blogs.msdn.com/b/alexj/archive/2009/06/02/tip-22-how-to-make-include-really-include.aspx

其中说我需要这样重写查询:

Which says i need to rewrite my query like this:

var companies = (from c in database.Companies.GetAll()
                 join l in database.CompanyList.GetAll() on c.ID equals l.CompanyID
                 where l.PortalID == 9 && l.BranchID == 1597
                 select c).Include("City").Take(10);


推荐答案

您的问题的答案为何忽略了Include在最后的表达式中操作?因为当您将 Companies 表与 CompanyList 联接时,此联接操作的结果实体类型是new类型,所以仅包含两个实体类型的原始类型属性,但是如果要包括城市,则必须将结果类型转换为公司类型,然后调用包含此转换类型的城市。

the answer to your question why is it ignoring the Include operation in the final expression? because when you join The Companies Table with CompanyList the result entity type of this join operation is new type contains both entities types primitive type properties only But if you want to include cities you have to cast result type to Company Type then call include City of this cast type.

这篇关于使用实体框架连接视图时使用.include()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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