Optimize entity framework query

前端 未结 5 1415
星月不相逢
星月不相逢 2020-12-02 12:27

I\'m trying to make a stackoverflow clone in my own time to learn EF6 and MVC5, i\'m currently using OWin for authentication.

Everything works fine when i have like

5条回答
  •  遥遥无期
    2020-12-02 13:19

    As you already know, Include method generate monstrous SQL.

    Disclaimer: I'm the owner of the project Entity Framework Plus (EF+)

    The EF+ Query IncludeOptimized method allows optimizing the SQL generated exactly like EF Core does.

    Instead of generating a monstrous SQL, multiple SQL are generated (one for each include). This feature also as a bonus, it allows filtering related entities.

    Docs: EF+ Query IncludeOptimized

    var query = ctx.Questions
                   .AsNoTracking()
                   .IncludeOptimized(x => x.Attachments)                                
                   .IncludeOptimized(x => x.Location)
                   .IncludeOptimized(x => x.CreatedBy) //IdentityUser
                   .IncludeOptimized(x => x.Tags)
                   .IncludeOptimized(x => x.Upvotes)
                   .IncludeOptimized(x => x.Upvotes.Select(y => y.CreatedBy))
                   .IncludeOptimized(x => x.Downvotes)
                   .IncludeOptimized(x => x.Downvotes.Select(y => y.CreatedBy))
                   .AsQueryable();
    

提交回复
热议问题