首页 技术 正文
技术 2022年11月16日
0 收藏 884 点赞 3,713 浏览 2122 个字

来自博客园开发团队开发前线最新消息:

在Entity Framework中,如果实体A关联了实体B,你想在加载实体A的同时加载实体B。通常做法是在LINQ查询中使用Include()。但是,如果你在查询条件中用到了实体B,EF会自动加载实体B,这时Include不仅是多余的,而且还会增加额外的LEFT OUTER JOIN查询,影响性能。

请看我们在博问开发中遭遇这个问题时的一段代码:

Entity Framework 使用注意:Where查询条件中用到的关联实体不需要Include

//For q.cnblogs.com
public class QuestionService
{
private IRepository<QuestionItem> _questionRepository; public QuestionService(IUnitOfWork unitOfWork)
: base(unitOfWork)
{
_questionRepository = new Repository<QuestionItem>(unitOfWork);
} public List<QuestionItem> GetUnsolvedQuestions(int pageIndex, int pageSize)
{
return _questionRepository.Entities
.Include(q => q.User)
.Where(q => q.IsActive && q.User.IsActive)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.ToList(); }
}public class QuestionItem
{
public int Id { get;set; }
public string Title { get; set; }
public bool IsActive { get; set; }
public int UserId { get; set; } public User User { get; set; }
}public class User
{
public int UserId { get; set; }
public bool IsActive {get;set;}
}

Entity Framework 使用注意:Where查询条件中用到的关联实体不需要Include

在上面的代码中,我们想在GetActiveQuestions()返回List<QuestionItem>时,QuestionItem中要包含User信息,所以在LINQ查询使用了.Include(q => q.User)。

(特别要注意的是:这里把q.User.IsActive作为查询条件之一)

然后我们用SQL Server Profiler发现,Entity Framework生成了如下的SQL语句:

Entity Framework 使用注意:Where查询条件中用到的关联实体不需要Include

SELECT TOP (25)
[Filter1].[Id] AS [Id],
[Filter1].[Title] AS [Title],
[Filter1].[UserId] AS [UserId],
[Filter1].[UserId1] AS [UserId1],
[Filter1].[IsActive1] AS [IsActive],
[Filter1].[UserId2] AS [UserId1],
[Filter1].[IsActive2] AS [IsActive1]
FROM ( SELECT [Extent1].[Id] AS [Id], [Extent1].[Title] AS [Title],
[Extent1].[UserId] AS [UserId1],[Extent1].[IsActive] AS [IsActive1],
[Extent3].[UserID] AS [UserID2], [Extent3].[IsActive] AS [IsActive2],
row_number() OVER (ORDER BY [Extent1].[QID] DESC) AS [row_number]
FROM [dbo].[question_Item] AS [Extent1]
INNER JOIN [dbo].[Users] AS [Extent2] ON [Extent1].[UserID] = [Extent2].[UserID]
LEFT OUTER JOIN [dbo].[Users] AS [Extent3] ON [Extent1].[UserID] = [Extent3].[UserID]
WHERE ([Extent1].[IsActive] = 1) AND ([Extent2].[IsActive] = 1)
) AS [Filter1]
WHERE [Filter1].[row_number] > 0
ORDER BY [Filter1].[Id] DESC

Entity Framework 使用注意:Where查询条件中用到的关联实体不需要Include

[dbo].[Users]表对应的就是User实体类,上面的SQL中[dbo].[Users]出现了两次JOIN,由于[Users]表数据量比较大,两次JOIN影响了执行计划,查询耗时增加。这显然是要避免的。

在与这个问题一阵激战之后,我们终于找到解决方法 —— 去掉Include,就这么简单!

从这个地方看,Entity Framework还是挺聪明的,但是由于不知道它的这个聪明之处,反而带来了问题。

所以,代码如人,要和她相处好,就要了解她的一切!

原文:http://www.cnblogs.com/dudu/archive/2012/04/13/entity_framework_include_where.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903