1
\$\begingroup\$

I'm very new to Entity Framework Core, so I'm concerned about whether I've done this query in the most efficient way possible.

Is there way to improve the code that uses Entity framework in .net core 6.0?

        var track = await this.DbContext.Tracks
           .Include(t => t.Specialities)
               .ThenInclude(s => s.Roles)
                    .ThenInclude(r => r.RoleEducation)
                        .ThenInclude(re => re.Education)
           .Include(t => t.Specialities)
               .ThenInclude(s => s.Roles)
                    .ThenInclude(r => r.RoleExperience)
                        .ThenInclude(re => re.Experience)
           .Include(t => t.Specialities)
               .ThenInclude(s => s.Roles)
                    .ThenInclude(r => r.SkillRoleRequirements)
                        .ThenInclude(s => s.MasteryLevel)
                            .ThenInclude(m => m.SkillType)
           .Include(t => t.Specialities)
               .ThenInclude(s => s.Roles)
                    .ThenInclude(r => r.SkillRoleRequirements)
                        .ThenInclude(s => s.Skill)
                            .ThenInclude(s => s.SkillType)
                     .FirstOrDefaultAsync(t => t.Id == id)
           .ConfigureAwait(false);
\$\endgroup\$
2
  • 1
    \$\begingroup\$ OnModelCreating there is AutoInclude option under Entity<TEntity>(x=> x.Navigation(...).AutoInclude()); in your case, it will take a couple of lines to include each navigation separately. but at the end it will get rid of all of these includes. \$\endgroup\$
    – iSR5
    Aug 5 at 7:53
  • \$\begingroup\$ @iSR5 - Thank you for your suggestion. \$\endgroup\$ 2 days ago

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.