实体框架中连接的约定6.1.0代码第一 [英] Conventions for joins in Entity Framework 6.1.0 Code First

查看:125
本文介绍了实体框架中连接的约定6.1.0代码第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我的问题很简单(不知道答案如何):

I believe my question is simple (not sure if the answer is as well):

有人知道如何强制实体框架使用INNER JOIN作为默认约定,而不是LEFT OUTER JOIN?

Does anybody know how to force Entity Framework to use "INNER JOIN" as default convention, instead of "LEFT OUTER JOIN"?

推荐答案

如果要明确强制执行内部或外部连接,您可以随时使用分别加入加入 GroupJoin 方法。 (或综合等价物加入 join ... into )。

If you want to enforce inner or outer joins explicitly you can always use the Join or GroupJoin methods, respectively. (Or the comprehensive equivalents join and join ... into).

但是,一般来说,在LINQ语句中,您应该避免使用显式连接语句。使用导航属性。导航属性是在实体之间定义的关联。通过导航属性进行查询是通过联接进行查询而无需对其进行手动编码。但是这些连接何时是内在或外在的?

But, generally speaking, in LINQ statements you should avoid using explicit join statements. Use navigation properties instead. Navigation properties are the associations that have been defined between entities. Querying by navigation properties is querying by joins without hand-coding them. But when will these joins be inner or outer?

采取这三个简单的类:

class Product
{
    public int Id { get; set; }
    public string Name { get; set; }

    public int CategoryId { get; set; }
    public Category Category { get; set; }

    public int? PhotoId { get; set; }
    public Photo Photo { get; set; }
}

class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Product> Products { get; set; }
}

class Photo
{
    public int Id { get; set; }
    public byte[] Image { get; set; }
}

如果我们查询产品,包括其类别和照片,会发生什么? p>

What will happen if we query products including their categories and photos?

context.Products.Include(p => p.Category).Include(p => p.Photo)

实体框架查看关联的基数,以确定是否可以生成优选内部联接或外部连接:

Entity Framework looks at the cardinality of an association to determine whether it can generate the preferable inner join, or else an outer join:


  • 所需的关联将生成内部连接。

  • 一个可选的关联会生成一个外部连接。

所以在这种情况下,它将生成一个内部连接, Product 类别产品照片

So in this case it will generate an inner join between Product and Category and an outer join between Product and Photo.

我认为这是一个明智的选择。如果在结果集中包含照片会突然减少获取的项目数量,这将是意想不到的行为。

I think this is a sensible choice. It would be unexpected behaviour if including photos in the result set would suddenly reduce the number of fetched items.

同样适用于其他查询形状,如

The same applies to other query shapes, like

context.Products.Select(p => new { p.Name, Cat = p.Category.Name })

这会产生一个内部连接。

This generates an inner join.

context.Products.Select(p => new { p.Name, Cat = p.Photo.Image })

这会产生一个外部连接。

This generates an outer join.

EF不够聪明(但是)看到它可以生成一个内部连接,如果在后一个例子,您只会查询 Photo!= null 的产品。

EF isn't smart enough (yet?) to see that it could generate an inner join if in the latter example you'd only query products where Photo != null. That could be a case where you'd want to write an explicit LINQ join.

所以这样关注导航属性会转移您的注意力。

So this focus on navigation properties shifts your attention from...


如何强制实体框架使用INNER JOIN

how to force Entity Framework to use "INNER JOIN"

...更多的业务逻辑相关决定是否需要关联或可选。

...to more business-logic related decisions whether associations should be required or optional.

这篇关于实体框架中连接的约定6.1.0代码第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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