2017-05-04 138 views
0

在此查詢在這裏,有沒有什麼辦法來減少加載多個相關的實體與實體框架的核心

  .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

如果你這樣做,會發生什麼呢?

.Include(c => c.EducationCredential.Concentration) 
    .Include(c => c.EducationCredential.School) 
    .Include(c => c.EducationCredential.SecondConcentration) 
+0

同樣的事情。我在github上爲實體框架創建了一個問題,這顯然是一個已知的問題。所以,我們只需要忍受它,直到它被修復或滾動您自己的查詢。 – Nihat