EF Core 3, optimize lots of Include/ThenInclude

前端 未结 4 896
予麋鹿
予麋鹿 2021-01-06 04:39

I have a query like this

return await _ctx.Activities
            .Include(a => a.Attributes)
            .Include(a => a.Roles)
            .Include(a         


        
4条回答
  •  孤街浪徒
    2021-01-06 05:19

    You'll want to split it into multiple queries, to speed up the performance. You can use explicit loading for this. It's not the prettiest solution, but it works. Hopefully an easier solution will come in EF 5.

    I'm guessing a bit on which fields are collections and which are "normal" entries, but something like this:

    var activity = await _ctx.Activities.FindAsync(Id);
    
    await context.Entry(activity)
        .Collection(a => a.Attributes)
        .LoadAsync();
    
    await context.Entry(activity)
        .Collection(a => a.Roles)
        .LoadAsync();
    
    await context.Entry(activity)
        .Collection(a => a.Bookmarks)
        .LoadAsync();
    
    await context.Entry(activity)
        .Collection(a => a.VideoMetas)
        .Query()
        .Include(vm => vm.Instances)
        .LoadAsync();
    
    await context.Entry(activity)
        .Collection(a => a.ImageMetas)
        .Query()
        .Include(im => im.Instances)
        .LoadAsync();
    
    await context.Entry(activity)
        .Reference(a => a.Procedure)
        .Query()
        .Include(p => p.Attributes)
        .LoadAsync();
    
    return activity;
    

提交回复
热议问题