EF不会加载所有子实体,即使包含

时间:2018-12-10 20:34:12

标签: c# entity-framework

当我加载数据时,它将返回我的所有条目,但是其中一些没有子元素,但是当我在数据库中查看时,引用键被设置为一个值。我找不到这是怎么回事。

几乎所有我的SEETwinning都有SpeakerConference,但有些甚至在数据库中都没有。

var query = dbContext.Set<SEETwinning>().AsQueryable()
                .Include(x => x.FollowUpCodes)
                .Include(x => x.SpeakerConference)
                .Include(x => x.SpeakerConference.SpeakerRequest)
                .Include(x => x.SpeakerConference.SpeakerRequest.Region)
                .Include(x => x.SpeakerConference.SpeakerRequest.School)
                .Include(x => x.SpeakerConference.SpeakerRequest.School.Institution)
                .Include(x => x.SpeakerEntrepreneurs);

public class SEETwinning : Entity
    {

        public virtual SpeakerConference SpeakerConference { get; set; }
        public virtual IList<SpeakerEntrepreneur> SpeakerEntrepreneurs { get; set; }
        public DateTime ConfirmedDate { get; set; }
        public SpeakerRequestDisponibilityDaytime ConfirmedDayTime { get; set; }
        public TimeSpan ConfirmedBeginTime { get; set; }
        public TimeSpan ConfirmedEndTime { get; set; }

        public virtual List<FollowUpCodesProjects> FollowUpCodes { get; set; }
        public string ConfirmationEmailSentDates { get; set; }
        public string ConfirmationRecallEmailSentDates { get; set; }
        public SEEProgress Progress { get; set; }
        public DateTime? MaterialSentDate { get; set; }
        public bool MaterialSent { get; set; }

        public int Number { get; set; }


    }

public class SpeakerConference : Entity
    {
        public virtual SpeakerRequest SpeakerRequest { get; set; }
        public virtual SEETwinning SEETwinning { get; set; }

        public SpeakerConferenceType Type { get; set; }
        public string SchoolLevels { get; set; }
        public SpeakerConferenceGender Gender { get; set; }
        public SpeakerConferenceLanguage Language { get; set; }

        public int StudentCount { get; set; }

        public string AreaOfInterest { get; set; }
        public bool NoAreaOfInterest { get; set; }

        public string FurtherInformation { get; set; }
        public bool NoFurtherInformation { get; set; }

        public string AccessibilityInformation { get; set; }

        public virtual IList<SpeakerRequestDisponibility> Disponibilities { get; set; }
        public SEEProgress SEEProgress { get; set; }

        public int RequestNumber { get; set; }

        public virtual List<FollowUpCodesProjects> FollowUpCodes { get; set; }

        public SpeakerConference()
        {
            Disponibilities = new List<SpeakerRequestDisponibility>();
            SEEProgress = SEEProgress.Production;
        }
    }


            modelBuilder.Entity<SpeakerConference>()
                .HasOptional(x => x.SEETwinning)
                .WithOptionalPrincipal(x => x.SpeakerConference);

            modelBuilder.Entity<SEETwinning>()
                .HasOptional(x => x.SpeakerConference)
                .WithOptionalDependent(x => x.SEETwinning);

编辑:添加了SQL查询

SELECT
[UnionAll1].[Id] AS [C1],
[UnionAll1].[Id1] AS [C2],
[UnionAll1].[Id2] AS [C3],
[UnionAll1].[Id3] AS [C4],
[UnionAll1].[ConfirmedDayTime] AS [C5],
[UnionAll1].[Id4] AS [C6],
[UnionAll1].[ConfirmedDate] AS [C7],
[UnionAll1].[ConfirmedDayTime1] AS [C8],
[UnionAll1].[ConfirmedBeginTime] AS [C9],
[UnionAll1].[ConfirmedEndTime] AS [C10],
[UnionAll1].[ConfirmationEmailSentDates] AS [C11],
[UnionAll1].[ConfirmationRecallEmailSentDates] AS [C12],
[UnionAll1].[Progress] AS [C13],
[UnionAll1].[MaterialSentDate] AS [C14],
[UnionAll1].[MaterialSent] AS [C15],
[UnionAll1].[Number1] AS [C16],
[UnionAll1].[NumberString] AS [C17],
[UnionAll1].[CreationDate] AS [C18],
[UnionAll1].[ModificationDate] AS [C19],
[UnionAll1].[DeactivatedOn] AS [C20],
[UnionAll1].[Active] AS [C21],
[UnionAll1].[ConfirmedDayTime2] AS [C22],
[UnionAll1].[Id5] AS [C23],
[UnionAll1].[Type] AS [C24],
[UnionAll1].[SchoolLevels] AS [C25],
[UnionAll1].[Gender] AS [C26],
[UnionAll1].[Language] AS [C27],
[UnionAll1].[StudentCount] AS [C28],
[UnionAll1].[AreaOfInterest] AS [C29],
[UnionAll1].[NoAreaOfInterest] AS [C30],
[UnionAll1].[FurtherInformation] AS [C31],
[UnionAll1].[NoFurtherInformation] AS [C32],
[UnionAll1].[AccessibilityInformation] AS [C33],
[UnionAll1].[SEEProgress] AS [C34],
[UnionAll1].[RequestNumber] AS [C35],
[UnionAll1].[CreationDate1] AS [C36],
[UnionAll1].[ModificationDate1] AS [C37],
[UnionAll1].[DeactivatedOn1] AS [C38],
[UnionAll1].[Active1] AS [C39],
[UnionAll1].[ConfirmedDayTime3] AS [C40],
[UnionAll1].[Id6] AS [C41],
[UnionAll1].[Number2] AS [C42],
[UnionAll1].[ResponsiblePerson_Title] AS [C43],
[UnionAll1].[ResponsiblePerson_Role] AS [C44],
[UnionAll1].[ResponsiblePerson_FirstName] AS [C45],
[UnionAll1].[ResponsiblePerson_LastName] AS [C46],
[UnionAll1].[ResponsiblePerson_Email] AS [C47],
[UnionAll1].[ResponsiblePerson_PrimaryPhoneNumber] AS [C48],
[UnionAll1].[ResponsiblePerson_SecondaryPhoneNumber] AS [C49],
[UnionAll1].[ResponsiblePerson_RoleString] AS [C50],
[UnionAll1].[Number3] AS [C51],
[UnionAll1].[ResponsiblePerson_Address_Street] AS [C52],
[UnionAll1].[ResponsiblePerson_Address_Street2] AS [C53],
[UnionAll1].[ResponsiblePerson_Address_City] AS [C54],
[UnionAll1].[ResponsiblePerson_Address_PostalCode] AS [C55],
[UnionAll1].[Active2] AS [C56],
[UnionAll1].[Number4] AS [C57],
[UnionAll1].[NumberString1] AS [C58],
[UnionAll1].[Language1] AS [C59],
[UnionAll1].[InstitutionId] AS [C60],
[UnionAll1].[RegionId] AS [C61],
[UnionAll1].[SchoolId] AS [C62],
[UnionAll1].[Progress1] AS [C63],
[UnionAll1].[LastCompletedStepIndex] AS [C64],
[UnionAll1].[IsConsent] AS [C65],
[UnionAll1].[DepositDate] AS [C66],
[UnionAll1].[CreationDate2] AS [C67],
[UnionAll1].[ModificationDate2] AS [C68],
[UnionAll1].[DeactivatedOn2] AS [C69],
[UnionAll1].[Id7] AS [C70],
[UnionAll1].[RegionType] AS [C71],
[UnionAll1].[Code] AS [C72],
[UnionAll1].[Name] AS [C73],
[UnionAll1].[Description] AS [C74],
[UnionAll1].[Note] AS [C75],
[UnionAll1].[CreationDate3] AS [C76],
[UnionAll1].[ModificationDate3] AS [C77],
[UnionAll1].[DeactivatedOn3] AS [C78],
[UnionAll1].[Active3] AS [C79],
[UnionAll1].[ConfirmedDayTime4] AS [C80],
[UnionAll1].[Id8] AS [C81],
[UnionAll1].[Name1] AS [C82],
[UnionAll1].[Type1] AS [C83],
[UnionAll1].[Address_Street] AS [C84],
[UnionAll1].[Address_Street2] AS [C85],
[UnionAll1].[Address_City] AS [C86],
[UnionAll1].[Address_PostalCode] AS [C87],
[UnionAll1].[Type2] AS [C88],
[UnionAll1].[Level] AS [C89],
[UnionAll1].[InstitutionId1] AS [C90],
[UnionAll1].[CreationDate4] AS [C91],
[UnionAll1].[ModificationDate4] AS [C92],
[UnionAll1].[DeactivatedOn4] AS [C93],
[UnionAll1].[Active4] AS [C94],
[UnionAll1].[ConfirmedDayTime5] AS [C95],
[UnionAll1].[Id9] AS [C96],
[UnionAll1].[Name2] AS [C97],
[UnionAll1].[Code1] AS [C98],
[UnionAll1].[CreationDate5] AS [C99],
[UnionAll1].[ModificationDate5] AS [C100],
[UnionAll1].[DeactivatedOn5] AS [C101],
[UnionAll1].[Active5] AS [C102],
[UnionAll1].[Region_Id] AS [C103],
[UnionAll1].[Participant_Id] AS [C104],
[UnionAll1].[Id10] AS [C105],
[UnionAll1].[C1] AS [C106],
[UnionAll1].[FollowUpCode] AS [C107],
[UnionAll1].[Id11] AS [C108],
[UnionAll1].[FollowUpCode1] AS [C109],
[UnionAll1].[CreationDate6] AS [C110],
[UnionAll1].[ModificationDate6] AS [C111],
[UnionAll1].[DeactivatedOn6] AS [C112],
[UnionAll1].[Active6] AS [C113],
[UnionAll1].[StudentEntrepreneurshipProject_Id] AS [C114],
[UnionAll1].[BusinessCreationProject_Id] AS [C115],
[UnionAll1].[E4Project_Id] AS [C116],
[UnionAll1].[E4Results_Id] AS [C117],
[UnionAll1].[SEETwinnings_Id] AS [C118],
[UnionAll1].[SpeakerConferences_Id] AS [C119],
[UnionAll1].[SpeakerRequest_Id] AS [C120],
[UnionAll1].[SpeakerEntrepreneurs_Id] AS [C121],
[UnionAll1].[User_Id] AS [C122],
...
[UnionAll1].[C61] AS [C182]
FROM (SELECT
CASE WHEN ([Extent10].[FollowUpCode] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1],
[Extent1].[Number] AS [Number],
[Extent2].[Id] AS [Id],
[Extent1].[Id] AS [Id1],
[Extent3].[Id] AS [Id2],
[Extent4].[Id] AS [Id3],
[Extent1].[ConfirmedDayTime] AS [ConfirmedDayTime],
[Extent1].[Id] AS [Id4],
[Extent1].[ConfirmedDate] AS [ConfirmedDate],
[Extent1].[ConfirmedDayTime] AS [ConfirmedDayTime1],
...
[Extent10].[BusinessCreationProject_Id] AS [BusinessCreationProject_Id],
[Extent10].[E4Project_Id] AS [E4Project_Id],
[Extent10].[E4Results_Id] AS [E4Results_Id],
[Extent10].[SEETwinnings_Id] AS [SEETwinnings_Id],
[Extent10].[SpeakerConferences_Id] AS [SpeakerConferences_Id],
[Extent10].[SpeakerRequest_Id] AS [SpeakerRequest_Id],
[Extent10].[SpeakerEntrepreneurs_Id] AS [SpeakerEntrepreneurs_Id],
[Extent10].[User_Id] AS [User_Id],
CAST(NULL AS int) AS [C2],
...
CAST(NULL AS uniqueidentifier) AS [C61]
FROM [dbo].[SEETwinnings] AS [Extent1]
LEFT OUTER JOIN [dbo].[SpeakerConferences] AS [Extent2] ON ([Extent2].[Id] = [Extent1].[SpeakerConference_Id]) AND ([Extent1].[SpeakerConference_Id] IS NOT NULL)
LEFT OUTER JOIN [dbo].[SpeakerRequests] AS [Extent3] ON [Extent2].[SpeakerRequest_Id] = [Extent3].[Id]
INNER JOIN [dbo].[Regions] AS [Extent4] ON [Extent3].[RegionId] = [Extent4].[Id]
LEFT OUTER JOIN [dbo].[SpeakerConferences] AS [Extent5] ON ([Extent5].[Id] = [Extent1].[SpeakerConference_Id]) AND ([Extent1].[SpeakerConference_Id] IS NOT NULL)
LEFT OUTER JOIN [dbo].[SpeakerRequests] AS [Extent6] ON [Extent5].[SpeakerRequest_Id] = [Extent6].[Id]
LEFT OUTER JOIN [dbo].[Regions] AS [Extent7] ON [Extent6].[RegionId] = [Extent7].[Id]
LEFT OUTER JOIN [dbo].[Schools] AS [Extent8] ON [Extent6].[SchoolId] = [Extent8].[Id]
LEFT OUTER JOIN [dbo].[Institutions] AS [Extent9] ON [Extent8].[InstitutionId] = [Extent9].[Id]
LEFT OUTER JOIN [dbo].[FollowUpCodesProjects] AS [Extent10] ON [Extent1].[Id] = [Extent10].[SEETwinnings_Id]
WHERE [Extent4].[Code] IN (3)
UNION ALL
SELECT
2 AS [C1],
[Extent11].[Number] AS [Number],
[Extent12].[Id] AS [Id],
[Extent11].[Id] AS [Id1],
[Extent13].[Id] AS [Id2],
[Extent14].[Id] AS [Id3],
[Extent11].[ConfirmedDayTime] AS [ConfirmedDayTime],
[Extent11].[Id] AS [Id4],
...
[Extent16].[Participant_Id] AS [Participant_Id],
[Extent11].[Id] AS [Id10],
CAST(NULL AS int) AS [C2],
...
CAST(NULL AS uniqueidentifier) AS [C17],
[Join18].[NbConferences] AS [NbConferences],
[Join18].[Id] AS [Id11],
[Join18].[NbConferences] AS [NbConferences1],
...
[Join18].[Participant_Id] AS [Participant_Id1]
FROM [dbo].[SEETwinnings] AS [Extent11]
LEFT OUTER JOIN [dbo].[SpeakerConferences] AS [Extent12] ON ([Extent12].[Id] = [Extent11].[SpeakerConference_Id]) AND ([Extent11].[SpeakerConference_Id] IS NOT NULL)
LEFT OUTER JOIN [dbo].[SpeakerRequests] AS [Extent13] ON [Extent12].[SpeakerRequest_Id] = [Extent13].[Id]
INNER JOIN [dbo].[Regions] AS [Extent14] ON [Extent13].[RegionId] = [Extent14].[Id]
LEFT OUTER JOIN [dbo].[SpeakerConferences] AS [Extent15] ON ([Extent15].[Id] = [Extent11].[SpeakerConference_Id]) AND ([Extent11].[SpeakerConference_Id] IS NOT NULL)
LEFT OUTER JOIN [dbo].[SpeakerRequests] AS [Extent16] ON [Extent15].[SpeakerRequest_Id] = [Extent16].[Id]
LEFT OUTER JOIN [dbo].[Regions] AS [Extent17] ON [Extent16].[RegionId] = [Extent17].[Id]
LEFT OUTER JOIN [dbo].[Schools] AS [Extent18] ON [Extent16].[SchoolId] = [Extent18].[Id]
LEFT OUTER JOIN [dbo].[Institutions] AS [Extent19] ON [Extent18].[InstitutionId] = [Extent19].[Id]
INNER JOIN (SELECT [Extent20].[SEETwinning_Id] AS [SEETwinning_Id], [Extent21].[Id] AS [Id], [Extent21].[Entrepreneur_Title] AS [Entrepreneur_Title], [Extent21].[Entrepreneur_Role] AS [Entrepreneur_Role], [Extent21].[Entrepreneur_FirstName] AS [Entrepreneur_FirstName], [Extent21].[Entrepreneur_LastName] AS [Entrepreneur_LastName], [Extent21].[Entrepreneur_Email] AS [Entrepreneur_Email], [Extent21].[Entrepreneur_PrimaryPhoneNumber] AS [Entrepreneur_PrimaryPhoneNumber], [Extent21].[Entrepreneur_SecondaryPhoneNumber] AS [Entrepreneur_SecondaryPhoneNumber], [Extent21].[Entrepreneur_RoleString] AS [Entrepreneur_RoleString], [Extent21].[Entrepreneur_Address_Street] AS [Entrepreneur_Address_Street], [Extent21].[Entrepreneur_Address_Street2] AS [Entrepreneur_Address_Street2], [Extent21].[Entrepreneur_Address_City] AS [Entrepreneur_Address_City], [Extent21].[Entrepreneur_Address_PostalCode] AS [Entrepreneur_Address_PostalCode], [Extent21].[EntrepreneurPresentation] AS [EntrepreneurPresentation], [Extent21].[EntrepreneursPictures_ContentType] AS [EntrepreneursPictures_ContentType], [Extent21].[EntrepreneursPictures_ContentLength] AS [EntrepreneursPictures_ContentLength], [Extent21].[EntrepreneursPictures_FileName] AS [EntrepreneursPictures_FileName], [Extent21].[EntrepreneursPictures_RelativeUrl] AS [EntrepreneursPictures_RelativeUrl], [Extent21].[EnterpriseLogo_ContentType] AS [EnterpriseLogo_ContentType], [Extent21].[EnterpriseLogo_ContentLength] AS [EnterpriseLogo_ContentLength], [Extent21].[EnterpriseLogo_FileName] AS [EnterpriseLogo_FileName], [Extent21].[EnterpriseLogo_RelativeUrl] AS [EnterpriseLogo_RelativeUrl], [Extent21].[BusinessName] AS [BusinessName], [Extent21].[BusinessDomain] AS [BusinessDomain], [Extent21].[BusinessSize] AS [BusinessSize], [Extent21].[BusinessWebsite] AS [BusinessWebsite], [Extent21].[BusinessAddress_Street] AS [BusinessAddress_Street], [Extent21].[BusinessAddress_Street2] AS [BusinessAddress_Street2], [Extent21].[BusinessAddress_City] AS [BusinessAddress_City], [Extent21].[BusinessAddress_PostalCode] AS [BusinessAddress_PostalCode], [Extent21].[GroupSize] AS [GroupSize], [Extent21].[NbConferences] AS [NbConferences], [Extent21].[SchoolLevel] AS [SchoolLevel], [Extent21].[DesiredSchools] AS [DesiredSchools], [Extent21].[DesiredSchoolsName] AS [DesiredSchoolsName], [Extent21].[TechnicalNeed] AS [TechnicalNeed], [Extent21].[TechnicalNeedOthers] AS [TechnicalNeedOthers], [Extent21].[SEEProgress] AS [SEEProgress], [Extent21].[ConferenceLanguage] AS [ConferenceLanguage], [Extent21].[Active] AS [Active], [Extent21].[Number] AS [Number], [Extent21].[NumberString] AS [NumberString], [Extent21].[Language] AS [Language], [Extent21].[InstitutionId] AS [InstitutionId], [Extent21].[RegionId] AS [RegionId], [Extent21].[SchoolId] AS [SchoolId], [Extent21].[Progress] AS [Progress], [Extent21].[LastCompletedStepIndex] AS [LastCompletedStepIndex], [Extent21].[IsConsent] AS [IsConsent], [Extent21].[DepositDate] AS [DepositDate], [Extent21].[CreationDate] AS [CreationDate], [Extent21].[ModificationDate] AS [ModificationDate], [Extent21].[DeactivatedOn] AS [DeactivatedOn], [Extent21].[Participant_Id] AS [Participant_Id]
FROM [dbo].[SEETwinningSpeakerEntrepreneurs] AS [Extent20]
INNER JOIN [dbo].[SpeakerEntrepreneurs] AS [Extent21] ON [Extent20].[SpeakerEntrepreneur_Id] = [Extent21].[Id] ) AS [Join18] ON [Extent11].[Id] = [Join18].[SEETwinning_Id]
WHERE [Extent14].[Code] IN (3)) AS [UnionAll1]
ORDER BY [UnionAll1].[Number] ASC, [UnionAll1].[Id] ASC, [UnionAll1].[Id1] ASC, [UnionAll1].[Id2] ASC, [UnionAll1].[Id3] ASC, [UnionAll1].[Id4] ASC, [UnionAll1].[Id5] ASC, [UnionAll1].[Id6] ASC, [UnionAll1].[Id7] ASC, [UnionAll1].[Id8] ASC, [UnionAll1].[Id9] ASC, [UnionAll1].[Id10] ASC, [UnionAll1].[C1] ASC

0 个答案:

没有答案