在此查询中,有没有办法减少
.Include( c => c.EducationCredential)
.ThenInclude(e => e.Concentration)
.Include( c => c.EducationCredential)
.ThenInclude(e => e.School)
.Include(c => c.EducationCredential)
.ThenInclude( e => e.SecondConcentration)
类似
.Include(c => c.EducationCredential and c.Concentration and c. e.SecondConcentration)
以下是完整版:
var cred = await _context.Credentials
.Where( c => c.CredentialId == credentialId)
.Include( c => c.EducationCredential)
.ThenInclude(e => e.Concentration)
.Include( c => c.EducationCredential)
.ThenInclude(e => e.School)
.Include(c => c.EducationCredential)
.ThenInclude( e => e.SecondConcentration)
.Include(c => c.LocationCredential)
.ThenInclude(lc => lc.Location)
.Include( c => c.LifeCredential)
.ThenInclude(c => c.Topic)
.Include(c => c.EmploymentCredential)
.ThenInclude(e => e.Company)
.FirstOrDefaultAsync();
return cred.Title;
这个生成不必要的连接,如下面sql查询中所示(注意e0,e1):
SELECT "c"."CredentialId", "c"."CredentialType", "c"."SorarakUserId", "c"."Title", "e"."EducationCredentialId", "e"."ConcentrationId", "e"."CredentialId", "e"."DegreeType", "e"."GraduationYear", "e"."SchoolId", "e"."SecondConcentrationId", "t"."TopicId", "t"."Category", "t"."Description", "t"."IconUrl", "t"."QuestionId", "t"."Title", "t"."TotalFollowers", "t"."UrlSlug", "e0"."EducationCredentialId", "e0"."ConcentrationId", "e0"."CredentialId", "e0"."DegreeType", "e0"."GraduationYear", "e0"."SchoolId", "e0"."SecondConcentrationId", "t0"."TopicId", "t0"."Category", "t0"."Description", "t0"."IconUrl", "t0"."QuestionId", "t0"."Title", "t0"."TotalFollowers", "t0"."UrlSlug", "e1"."EducationCredentialId", "e1"."ConcentrationId", "e1"."CredentialId", "e1"."DegreeType", "e1"."GraduationYear", "e1"."SchoolId", "e1"."SecondConcentrationId", "t1"."TopicId", "t1"."Category", "t1"."Description", "t1"."IconUrl", "t1"."QuestionId", "t1"."Title", "t1"."TotalFollowers", "t1"."UrlSlug", "l"."LocationCredentialId", "l"."CredentialId", "l"."EndYear", "l"."IsCurrentlyLivingThere", "l"."LocationId", "l"."StartYear", "t2"."TopicId", "t2"."Category", "t2"."Description", "t2"."IconUrl", "t2"."QuestionId", "t2"."Title", "t2"."TotalFollowers", "t2"."UrlSlug", "l0"."LifeCredentialId", "l0"."CredentialId", "l0"."TopicId", "t3"."TopicId", "t3"."Category", "t3"."Description", "t3"."IconUrl", "t3"."QuestionId", "t3"."Title", "t3"."TotalFollowers", "t3"."UrlSlug", "e2"."EmploymentCredentialId", "e2"."CompanyId", "e2"."CredentialId", "e2"."EndYear", "e2"."IsCurrentlyWorkingThere", "e2"."PositionTitle", "e2"."StartYear", "t4"."TopicId", "t4"."Category", "t4"."Description", "t4"."IconUrl", "t4"."QuestionId", "t4"."Title", "t4"."TotalFollowers", "t4"."UrlSlug"
FROM "Credentials" AS "c"
LEFT JOIN "EducationCredentials" AS "e" ON "e"."CredentialId" = "c"."CredentialId"
LEFT JOIN "Topics" AS "t" ON "e"."ConcentrationId" = "t"."TopicId"
LEFT JOIN "EducationCredentials" AS "e0" ON "e0"."CredentialId" = "c"."CredentialId"
LEFT JOIN "Topics" AS "t0" ON "e0"."SecondConcentrationId" = "t0"."TopicId"
LEFT JOIN "EducationCredentials" AS "e1" ON "e1"."CredentialId" = "c"."CredentialId"
LEFT JOIN "Topics" AS "t1" ON "e1"."SchoolId" = "t1"."TopicId"
LEFT JOIN "LocationCredentials" AS "l" ON "l"."CredentialId" = "c"."CredentialId"
LEFT JOIN "Topics" AS "t2" ON "l"."LocationId" = "t2"."TopicId"
LEFT JOIN "LifeCredentials" AS "l0" ON "l0"."CredentialId" = "c"."CredentialId"
LEFT JOIN "Topics" AS "t3" ON "l0"."TopicId" = "t3"."TopicId"
LEFT JOIN "EmploymentCredentials" AS "e2" ON "e2"."CredentialId" = "c"."CredentialId"
LEFT JOIN "Topics" AS "t4" ON "e2"."CompanyId" = "t4"."TopicId"
WHERE "c"."CredentialId" = $1
LIMIT 1
答案 0 :(得分:0)
如果你这样做会怎么样?
.Include( c => c.EducationCredential.Concentration)
.Include( c => c.EducationCredential.School)
.Include(c => c.EducationCredential.SecondConcentration)
答案 1 :(得分:0)
这是Entity Framework Core的一个问题,暂时没有解决方法。见https://github.com/aspnet/EntityFramework/issues/4900