引擎盖下了解EF。包括VS加盟 [英] Understanding EF under the hood. Include vs Joins

查看:117
本文介绍了引擎盖下了解EF。包括VS加盟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与实体框架工作了一段时间,但仍然有一些黑洞与它是如何引擎盖下工作。即使读几篇文章我不知道我的理解是否正确后

I am working with Entity Framework for a while but still have some "black holes" with how it is working under the hood. Even after reading couple of articles I'm not sure that my understanding is correct.

让我们开始的问题:


  • EF如何包含引擎盖?

  • 什么是加入和包含的区别?
  • 与包括性能问题(延迟加载VS预先加载,生成的SQL查询,等等)?

  • 什么时候应该使用加入,而不是包含反之亦然?

  • How EF Include is working under the hood?
  • What are the differences between Join and Include?
  • Performance issues with Include (lazy loading vs eager loading, generated SQL queries, etc.)?
  • When I should use Join instead Include and vice versa?

推荐答案

在通过LINQ或lambda表达式查询EF,你只需要如果连接语句基础架构不提供FKS,因此您不必在对象上的导航属性。

When querying EF through linq or lambda expressions, you only need join statements if the underlying schema doesn't provide FKs, and thus you don't have navigation properties on the objects.

在另一边,包括(立即加载)和延迟加载可以仅当有FKS,因为它使用的导航性能运行。

On the other side, include (eager loading) and lazy loading can only work if there are FKs, because it uses the navigation properties.

在这两种情况下底层SQL将使用连接(如SQL没有导航属性的概念)。

The underlying sql in both cases will use joins (as sql has no "navigation property" concept).

至于性能,这取决于情况。延迟加载VS预先加载(所以在FK情况下)可以是一个艰难的选择。

As for performance, it depends on situations. Lazy loading vs Eager loading (so in FK scenario) can be a difficult choice.

我平时懒加载的,有用的,当你有一个大的主要结果,但你去需要加入只是整个结果集的几个项目的数据。

I usually go with lazy loading, useful when you have a large main result, but you need "join" data only of a few items of the whole resultset.

如果你提前知道,你需要的连接整个结果集的数据,渴望能装为更好的性能。我建议进行试验,看看自己。

If you know ahead that you'll need the join data of the whole resultset, eager loading could be better for performance. I'd suggest to experiment and see for yourself.

这篇关于引擎盖下了解EF。包括VS加盟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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