日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/4/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Entity Framework 使用注意:Where查询条件中用到的关联实体不需要Include 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

來自博客園開發團隊開發前線最新消息:

在Entity Framework中,如果實體A關聯了實體B,你想在加載實體A的同時加載實體B。通常做法是在LINQ查詢中使用Include()。但是,如果你在查詢條件中用到了實體B,EF會自動加載實體B,這時Include不僅是多余的,而且還會增加額外的LEFT OUTER JOIN查詢,影響性能。?

請看我們在博問開發中遭遇這個問題時的一段代碼:

//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;} }

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

(特別要注意的是:這里把q.User.IsActive作為查詢條件之一)

然后我們用SQL Server Profiler發現,Entity Framework生成了如下的SQL語句:

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

[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

轉載于:https://www.cnblogs.com/lenther2002/p/4614758.html

總結

以上是生活随笔為你收集整理的Entity Framework 使用注意:Where查询条件中用到的关联实体不需要Include的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。