Entity Framework 6.1.0 Code First 中的连接约定 [英] Conventions for joins in Entity Framework 6.1.0 Code First

查看:22
本文介绍了Entity Framework 6.1.0 Code First 中的连接约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我的问题很简单(不确定答案是否也是如此):

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"?

推荐答案

如果你想显式地强制执行内部或外部连接,你总是可以使用 JoinGroupJoin 方法, 分别.(或综合等价物 joinjoin ... 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; }
}

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

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:

  • 所需的关联会生成内部连接.
  • 可选关联会生成外连接.

所以在这种情况下,它将在 ProductCategory 之间生成一个内连接,并在 ProductPhoto<之间生成一个外连接/代码>.

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 })

这会生成一个外连接.

如果在后一个示例中您只查询 Photo != null 的产品,EF 还不够聪明(还没有?)无法看到它可以生成内部联接.这可能是您想要编写显式 LINQ 连接的情况.

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.

这篇关于Entity Framework 6.1.0 Code First 中的连接约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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