0

我有一个表请求者,它有五个地址表的外键:

CREATE TABLE [dbo].[Requester](
    [Id] [int] NOT NULL,
    [FirstName] [nvarchar](64) NULL,
    [Surname] [nvarchar](64) NOT NULL,
    [**RegisteredAddress**] [int] NULL,
    [**MailingAddress**] [int] NULL,
    [**ResidenceAddress**] [int] NULL,
    [**CompanySeatAddress**] [int] NULL,
    [**WorkplaceAddress**] [int] NULL,
)

地址是一个简单的表

CREATE TABLE [dbo].[Address](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [AddressLine1] [nvarchar](64) NOT NULL,
    [AddressLine2] [nvarchar](64) NULL,
    [ZipCode] [varchar](6) NOT NULL,
    [Post] [nvarchar](64) NOT NULL,
)

我的 EF Code First 映射看起来像

 modelBuilder.Entity<Requester>()
        .HasOptional(a => a.RegisteredAddress).WithOptionalDependent().Map(x => x.MapKey("RegisteredAddress"));                                 

      modelBuilder.Entity<Requester>()
        .HasOptional(a => a.ResidenceAddress).WithOptionalDependent().Map(x => x.MapKey("ResidenceAddress"));  

      modelBuilder.Entity<Requester>()
       .HasOptional(a => a.MailingAddress).WithOptionalDependent().Map(x => x.MapKey("MailingAddress"));  

      modelBuilder.Entity<Requester>()
        .HasOptional(a => a.CompanySeatAddress).WithOptionalDependent().Map(x => x.MapKey("CompanySeatAddress"));  

      modelBuilder.Entity<Requester>()
     .HasOptional(a => a.WorkplaceAddress).WithOptionalDependent().Map(x => x.MapKey("WorkplaceAddress"));  

当我尝试使用急切加载进行查询时:

var requesters = (from x in _context.Requester.Include(x => x.MailingAddress)
                                        .Include(x => x.ResidenceAddress)
                                        .Include(x => x.RegisteredAddress)
                                        .Include(x => x.WorkplaceAddress)
                      select x).ToList();

生成了大量低效的 t-sql。

谁能建议如何优化这个?

SELECT [Extent1].[Id] AS [Id],
    [Extent1].[FirstName] AS [FirstName],
    [Extent1].[Surname] AS [Surname],
    [Join5].[Id1] AS [Id1],
    [Join5].[AddressLine1] AS [AddressLine1],
    [Join5].[AddressLine2] AS [AddressLine2],
    [Join5].[ZipCode] AS [ZipCode],
    [Join5].[Post] AS [Post],
    [Join11].[Id2] AS [Id2],
    [Join17].[Id3] AS [Id3],
    [Join23].[Id4] AS [Id4],
    [Join29].[Id5] AS [Id5],
    [Join35].[Id6] AS [Id6],
    [Join41].[Id7] AS [Id7],
    [Join41].[AddressLine1] AS [AddressLine11],
    [Join41].[AddressLine2] AS [AddressLine21],
    [Join41].[ZipCode] AS [ZipCode1],
    [Join41].[Post] AS [Post1],
    [Join47].[Id8] AS [Id8],
    [Join53].[Id9] AS [Id9],
    [Join59].[Id10] AS [Id10],
    [Join65].[Id11] AS [Id11],
    [Join71].[Id12] AS [Id12],
    [Join77].[Id13] AS [Id13],
    [Join77].[AddressLine1] AS [AddressLine12],
    [Join77].[AddressLine2] AS [AddressLine22],
    [Join77].[ZipCode] AS [ZipCode2],
    [Join77].[Post] AS [Post2],
    [Join83].[Id14] AS [Id14],
    [Join89].[Id15] AS [Id15],
    [Join95].[Id16] AS [Id16],
    [Join101].[Id17] AS [Id17],
    [Join107].[Id18] AS [Id18],
    [Join112].[Id19] AS [Id19],
    [Join112].[AddressLine1] AS [AddressLine13],
    [Join112].[AddressLine2] AS [AddressLine23],
    [Join112].[ZipCode] AS [ZipCode3],
    [Join112].[Post] AS [Post3],
    [Join117].[Id20] AS [Id20],
    [Join122].[Id21] AS [Id21],
    [Join127].[Id22] AS [Id22],
    [Join132].[Id23] AS [Id23],
    [Extent1].[CompanySeatAddress] AS [CompanySeatAddress]
FROM [dbo].[Requester] AS [Extent1]
LEFT JOIN (
    SELECT [Extent2].[Id] AS [Id1],
        [Extent2].[AddressLine1] AS [AddressLine1],
        [Extent2].[AddressLine2] AS [AddressLine2],
        [Extent2].[ZipCode] AS [ZipCode],
        [Extent2].[Post] AS [Post],
        [Extent5].[Id] AS [Id24]
    FROM [dbo].[Address] AS [Extent2]
    LEFT JOIN [dbo].[Requester] AS [Extent3] ON ([Extent2].[Id] = [Extent3].[RegisteredAddress])
        AND ([Extent3].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent4] ON ([Extent2].[Id] = [Extent4].[ResidenceAddress])
        AND ([Extent4].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent5] ON ([Extent2].[Id] = [Extent5].[MailingAddress])
        AND ([Extent5].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent6] ON ([Extent2].[Id] = [Extent6].[CompanySeatAddress])
        AND ([Extent6].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent7] ON ([Extent2].[Id] = [Extent7].[WorkplaceAddress])
        AND ([Extent7].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join5] ON [Extent1].[Id] = [Join5].[Id24]
LEFT JOIN (
    SELECT [Extent9].[Id] AS [Id2],
        [Extent11].[Id] AS [Id25]
    FROM [dbo].[Address] AS [Extent8]
    LEFT JOIN [dbo].[Requester] AS [Extent9] ON ([Extent8].[Id] = [Extent9].[RegisteredAddress])
        AND ([Extent9].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent10] ON ([Extent8].[Id] = [Extent10].[ResidenceAddress])
        AND ([Extent10].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent11] ON ([Extent8].[Id] = [Extent11].[MailingAddress])
        AND ([Extent11].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent12] ON ([Extent8].[Id] = [Extent12].[CompanySeatAddress])
        AND ([Extent12].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent13] ON ([Extent8].[Id] = [Extent13].[WorkplaceAddress])
        AND ([Extent13].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join11] ON [Extent1].[Id] = [Join11].[Id25]
LEFT JOIN (
    SELECT [Extent16].[Id] AS [Id3],
        [Extent17].[Id] AS [Id26]
    FROM [dbo].[Address] AS [Extent14]
    LEFT JOIN [dbo].[Requester] AS [Extent15] ON ([Extent14].[Id] = [Extent15].[RegisteredAddress])
        AND ([Extent15].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent16] ON ([Extent14].[Id] = [Extent16].[ResidenceAddress])
        AND ([Extent16].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent17] ON ([Extent14].[Id] = [Extent17].[MailingAddress])
        AND ([Extent17].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent18] ON ([Extent14].[Id] = [Extent18].[CompanySeatAddress])
        AND ([Extent18].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent19] ON ([Extent14].[Id] = [Extent19].[WorkplaceAddress])
        AND ([Extent19].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join17] ON [Extent1].[Id] = [Join17].[Id26]
LEFT JOIN (
    SELECT [Extent23].[Id] AS [Id4]
    FROM [dbo].[Address] AS [Extent20]
    LEFT JOIN [dbo].[Requester] AS [Extent21] ON ([Extent20].[Id] = [Extent21].[RegisteredAddress])
        AND ([Extent21].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent22] ON ([Extent20].[Id] = [Extent22].[ResidenceAddress])
        AND ([Extent22].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent23] ON ([Extent20].[Id] = [Extent23].[MailingAddress])
        AND ([Extent23].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent24] ON ([Extent20].[Id] = [Extent24].[CompanySeatAddress])
        AND ([Extent24].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent25] ON ([Extent20].[Id] = [Extent25].[WorkplaceAddress])
        AND ([Extent25].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join23] ON [Extent1].[Id] = [Join23].[Id4]
LEFT JOIN (
    SELECT [Extent29].[Id] AS [Id27],
        [Extent30].[Id] AS [Id5]
    FROM [dbo].[Address] AS [Extent26]
    LEFT JOIN [dbo].[Requester] AS [Extent27] ON ([Extent26].[Id] = [Extent27].[RegisteredAddress])
        AND ([Extent27].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent28] ON ([Extent26].[Id] = [Extent28].[ResidenceAddress])
        AND ([Extent28].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent29] ON ([Extent26].[Id] = [Extent29].[MailingAddress])
        AND ([Extent29].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent30] ON ([Extent26].[Id] = [Extent30].[CompanySeatAddress])
        AND ([Extent30].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent31] ON ([Extent26].[Id] = [Extent31].[WorkplaceAddress])
        AND ([Extent31].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join29] ON [Extent1].[Id] = [Join29].[Id27]
LEFT JOIN (
    SELECT [Extent35].[Id] AS [Id28],
        [Extent37].[Id] AS [Id6]
    FROM [dbo].[Address] AS [Extent32]
    LEFT JOIN [dbo].[Requester] AS [Extent33] ON ([Extent32].[Id] = [Extent33].[RegisteredAddress])
        AND ([Extent33].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent34] ON ([Extent32].[Id] = [Extent34].[ResidenceAddress])
        AND ([Extent34].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent35] ON ([Extent32].[Id] = [Extent35].[MailingAddress])
        AND ([Extent35].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent36] ON ([Extent32].[Id] = [Extent36].[CompanySeatAddress])
        AND ([Extent36].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent37] ON ([Extent32].[Id] = [Extent37].[WorkplaceAddress])
        AND ([Extent37].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join35] ON [Extent1].[Id] = [Join35].[Id28]
LEFT JOIN (
    SELECT [Extent38].[Id] AS [Id7],
        [Extent38].[AddressLine1] AS [AddressLine1],
        [Extent38].[AddressLine2] AS [AddressLine2],
        [Extent38].[ZipCode] AS [ZipCode],
        [Extent38].[Post] AS [Post],
        [Extent40].[Id] AS [Id29]
    FROM [dbo].[Address] AS [Extent38]
    LEFT JOIN [dbo].[Requester] AS [Extent39] ON ([Extent38].[Id] = [Extent39].[RegisteredAddress])
        AND ([Extent39].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent40] ON ([Extent38].[Id] = [Extent40].[ResidenceAddress])
        AND ([Extent40].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent41] ON ([Extent38].[Id] = [Extent41].[MailingAddress])
        AND ([Extent41].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent42] ON ([Extent38].[Id] = [Extent42].[CompanySeatAddress])
        AND ([Extent42].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent43] ON ([Extent38].[Id] = [Extent43].[WorkplaceAddress])
        AND ([Extent43].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join41] ON [Extent1].[Id] = [Join41].[Id29]
LEFT JOIN (
    SELECT [Extent45].[Id] AS [Id8],
        [Extent46].[Id] AS [Id30]
    FROM [dbo].[Address] AS [Extent44]
    LEFT JOIN [dbo].[Requester] AS [Extent45] ON ([Extent44].[Id] = [Extent45].[RegisteredAddress])
        AND ([Extent45].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent46] ON ([Extent44].[Id] = [Extent46].[ResidenceAddress])
        AND ([Extent46].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent47] ON ([Extent44].[Id] = [Extent47].[MailingAddress])
        AND ([Extent47].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent48] ON ([Extent44].[Id] = [Extent48].[CompanySeatAddress])
        AND ([Extent48].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent49] ON ([Extent44].[Id] = [Extent49].[WorkplaceAddress])
        AND ([Extent49].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join47] ON [Extent1].[Id] = [Join47].[Id30]
LEFT JOIN (
    SELECT [Extent52].[Id] AS [Id9]
    FROM [dbo].[Address] AS [Extent50]
    LEFT JOIN [dbo].[Requester] AS [Extent51] ON ([Extent50].[Id] = [Extent51].[RegisteredAddress])
        AND ([Extent51].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent52] ON ([Extent50].[Id] = [Extent52].[ResidenceAddress])
        AND ([Extent52].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent53] ON ([Extent50].[Id] = [Extent53].[MailingAddress])
        AND ([Extent53].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent54] ON ([Extent50].[Id] = [Extent54].[CompanySeatAddress])
        AND ([Extent54].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent55] ON ([Extent50].[Id] = [Extent55].[WorkplaceAddress])
        AND ([Extent55].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join53] ON [Extent1].[Id] = [Join53].[Id9]
LEFT JOIN (
    SELECT [Extent58].[Id] AS [Id31],
        [Extent59].[Id] AS [Id10]
    FROM [dbo].[Address] AS [Extent56]
    LEFT JOIN [dbo].[Requester] AS [Extent57] ON ([Extent56].[Id] = [Extent57].[RegisteredAddress])
        AND ([Extent57].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent58] ON ([Extent56].[Id] = [Extent58].[ResidenceAddress])
        AND ([Extent58].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent59] ON ([Extent56].[Id] = [Extent59].[MailingAddress])
        AND ([Extent59].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent60] ON ([Extent56].[Id] = [Extent60].[CompanySeatAddress])
        AND ([Extent60].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent61] ON ([Extent56].[Id] = [Extent61].[WorkplaceAddress])
        AND ([Extent61].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join59] ON [Extent1].[Id] = [Join59].[Id31]
LEFT JOIN (
    SELECT [Extent64].[Id] AS [Id32],
        [Extent66].[Id] AS [Id11]
    FROM [dbo].[Address] AS [Extent62]
    LEFT JOIN [dbo].[Requester] AS [Extent63] ON ([Extent62].[Id] = [Extent63].[RegisteredAddress])
        AND ([Extent63].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent64] ON ([Extent62].[Id] = [Extent64].[ResidenceAddress])
        AND ([Extent64].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent65] ON ([Extent62].[Id] = [Extent65].[MailingAddress])
        AND ([Extent65].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent66] ON ([Extent62].[Id] = [Extent66].[CompanySeatAddress])
        AND ([Extent66].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent67] ON ([Extent62].[Id] = [Extent67].[WorkplaceAddress])
        AND ([Extent67].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join65] ON [Extent1].[Id] = [Join65].[Id32]
LEFT JOIN (
    SELECT [Extent70].[Id] AS [Id33],
        [Extent73].[Id] AS [Id12]
    FROM [dbo].[Address] AS [Extent68]
    LEFT JOIN [dbo].[Requester] AS [Extent69] ON ([Extent68].[Id] = [Extent69].[RegisteredAddress])
        AND ([Extent69].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent70] ON ([Extent68].[Id] = [Extent70].[ResidenceAddress])
        AND ([Extent70].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent71] ON ([Extent68].[Id] = [Extent71].[MailingAddress])
        AND ([Extent71].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent72] ON ([Extent68].[Id] = [Extent72].[CompanySeatAddress])
        AND ([Extent72].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent73] ON ([Extent68].[Id] = [Extent73].[WorkplaceAddress])
        AND ([Extent73].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join71] ON [Extent1].[Id] = [Join71].[Id33]
LEFT JOIN (
    SELECT [Extent74].[Id] AS [Id13],
        [Extent74].[AddressLine1] AS [AddressLine1],
        [Extent74].[AddressLine2] AS [AddressLine2],
        [Extent74].[ZipCode] AS [ZipCode],
        [Extent74].[Post] AS [Post],
        [Extent75].[Id] AS [Id34]
    FROM [dbo].[Address] AS [Extent74]
    LEFT JOIN [dbo].[Requester] AS [Extent75] ON ([Extent74].[Id] = [Extent75].[RegisteredAddress])
        AND ([Extent75].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent76] ON ([Extent74].[Id] = [Extent76].[ResidenceAddress])
        AND ([Extent76].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent77] ON ([Extent74].[Id] = [Extent77].[MailingAddress])
        AND ([Extent77].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent78] ON ([Extent74].[Id] = [Extent78].[CompanySeatAddress])
        AND ([Extent78].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent79] ON ([Extent74].[Id] = [Extent79].[WorkplaceAddress])
        AND ([Extent79].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join77] ON [Extent1].[Id] = [Join77].[Id34]
LEFT JOIN (
    SELECT [Extent81].[Id] AS [Id14]
    FROM [dbo].[Address] AS [Extent80]
    LEFT JOIN [dbo].[Requester] AS [Extent81] ON ([Extent80].[Id] = [Extent81].[RegisteredAddress])
        AND ([Extent81].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent82] ON ([Extent80].[Id] = [Extent82].[ResidenceAddress])
        AND ([Extent82].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent83] ON ([Extent80].[Id] = [Extent83].[MailingAddress])
        AND ([Extent83].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent84] ON ([Extent80].[Id] = [Extent84].[CompanySeatAddress])
        AND ([Extent84].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent85] ON ([Extent80].[Id] = [Extent85].[WorkplaceAddress])
        AND ([Extent85].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join83] ON [Extent1].[Id] = [Join83].[Id14]
LEFT JOIN (
    SELECT [Extent87].[Id] AS [Id35],
        [Extent88].[Id] AS [Id15]
    FROM [dbo].[Address] AS [Extent86]
    LEFT JOIN [dbo].[Requester] AS [Extent87] ON ([Extent86].[Id] = [Extent87].[RegisteredAddress])
        AND ([Extent87].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent88] ON ([Extent86].[Id] = [Extent88].[ResidenceAddress])
        AND ([Extent88].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent89] ON ([Extent86].[Id] = [Extent89].[MailingAddress])
        AND ([Extent89].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent90] ON ([Extent86].[Id] = [Extent90].[CompanySeatAddress])
        AND ([Extent90].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent91] ON ([Extent86].[Id] = [Extent91].[WorkplaceAddress])
        AND ([Extent91].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join89] ON [Extent1].[Id] = [Join89].[Id35]
LEFT JOIN (
    SELECT [Extent93].[Id] AS [Id36],
        [Extent95].[Id] AS [Id16]
    FROM [dbo].[Address] AS [Extent92]
    LEFT JOIN [dbo].[Requester] AS [Extent93] ON ([Extent92].[Id] = [Extent93].[RegisteredAddress])
        AND ([Extent93].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent94] ON ([Extent92].[Id] = [Extent94].[ResidenceAddress])
        AND ([Extent94].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent95] ON ([Extent92].[Id] = [Extent95].[MailingAddress])
        AND ([Extent95].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent96] ON ([Extent92].[Id] = [Extent96].[CompanySeatAddress])
        AND ([Extent96].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent97] ON ([Extent92].[Id] = [Extent97].[WorkplaceAddress])
        AND ([Extent97].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join95] ON [Extent1].[Id] = [Join95].[Id36]
LEFT JOIN (
    SELECT [Extent99].[Id] AS [Id37],
        [Extent102].[Id] AS [Id17]
    FROM [dbo].[Address] AS [Extent98]
    LEFT JOIN [dbo].[Requester] AS [Extent99] ON ([Extent98].[Id] = [Extent99].[RegisteredAddress])
        AND ([Extent99].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent100] ON ([Extent98].[Id] = [Extent100].[ResidenceAddress])
        AND ([Extent100].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent101] ON ([Extent98].[Id] = [Extent101].[MailingAddress])
        AND ([Extent101].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent102] ON ([Extent98].[Id] = [Extent102].[CompanySeatAddress])
        AND ([Extent102].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent103] ON ([Extent98].[Id] = [Extent103].[WorkplaceAddress])
        AND ([Extent103].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join101] ON [Extent1].[Id] = [Join101].[Id37]
LEFT JOIN (
    SELECT [Extent105].[Id] AS [Id38],
        [Extent109].[Id] AS [Id18]
    FROM [dbo].[Address] AS [Extent104]
    LEFT JOIN [dbo].[Requester] AS [Extent105] ON ([Extent104].[Id] = [Extent105].[RegisteredAddress])
        AND ([Extent105].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent106] ON ([Extent104].[Id] = [Extent106].[ResidenceAddress])
        AND ([Extent106].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent107] ON ([Extent104].[Id] = [Extent107].[MailingAddress])
        AND ([Extent107].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent108] ON ([Extent104].[Id] = [Extent108].[CompanySeatAddress])
        AND ([Extent108].[CompanySeatAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent109] ON ([Extent104].[Id] = [Extent109].[WorkplaceAddress])
        AND ([Extent109].[WorkplaceAddress] IS NOT NULL)
    ) AS [Join107] ON [Extent1].[Id] = [Join107].[Id38]
LEFT JOIN (
    SELECT [Extent110].[Id] AS [Id19],
        [Extent110].[AddressLine1] AS [AddressLine1],
        [Extent110].[AddressLine2] AS [AddressLine2],
        [Extent110].[ZipCode] AS [ZipCode],
        [Extent110].[Post] AS [Post]
    FROM [dbo].[Address] AS [Extent110]
    LEFT JOIN [dbo].[Requester] AS [Extent111] ON ([Extent110].[Id] = [Extent111].[RegisteredAddress])
        AND ([Extent111].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent112] ON ([Extent110].[Id] = [Extent112].[ResidenceAddress])
        AND ([Extent112].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent113] ON ([Extent110].[Id] = [Extent113].[MailingAddress])
        AND ([Extent113].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent114] ON ([Extent110].[Id] = [Extent114].[CompanySeatAddress])
        AND ([Extent114].[CompanySeatAddress] IS NOT NULL)
    ) AS [Join112] ON ([Join112].[Id19] = [Extent1].[WorkplaceAddress])
    AND ([Extent1].[WorkplaceAddress] IS NOT NULL)
LEFT JOIN (
    SELECT [Extent115].[Id] AS [Id39],
        [Extent116].[Id] AS [Id20]
    FROM [dbo].[Address] AS [Extent115]
    LEFT JOIN [dbo].[Requester] AS [Extent116] ON ([Extent115].[Id] = [Extent116].[RegisteredAddress])
        AND ([Extent116].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent117] ON ([Extent115].[Id] = [Extent117].[ResidenceAddress])
        AND ([Extent117].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent118] ON ([Extent115].[Id] = [Extent118].[MailingAddress])
        AND ([Extent118].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent119] ON ([Extent115].[Id] = [Extent119].[CompanySeatAddress])
        AND ([Extent119].[CompanySeatAddress] IS NOT NULL)
    ) AS [Join117] ON ([Join117].[Id39] = [Extent1].[WorkplaceAddress])
    AND ([Extent1].[WorkplaceAddress] IS NOT NULL)
LEFT JOIN (
    SELECT [Extent120].[Id] AS [Id40],
        [Extent122].[Id] AS [Id21]
    FROM [dbo].[Address] AS [Extent120]
    LEFT JOIN [dbo].[Requester] AS [Extent121] ON ([Extent120].[Id] = [Extent121].[RegisteredAddress])
        AND ([Extent121].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent122] ON ([Extent120].[Id] = [Extent122].[ResidenceAddress])
        AND ([Extent122].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent123] ON ([Extent120].[Id] = [Extent123].[MailingAddress])
        AND ([Extent123].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent124] ON ([Extent120].[Id] = [Extent124].[CompanySeatAddress])
        AND ([Extent124].[CompanySeatAddress] IS NOT NULL)
    ) AS [Join122] ON ([Join122].[Id40] = [Extent1].[WorkplaceAddress])
    AND ([Extent1].[WorkplaceAddress] IS NOT NULL)
LEFT JOIN (
    SELECT [Extent125].[Id] AS [Id41],
        [Extent128].[Id] AS [Id22]
    FROM [dbo].[Address] AS [Extent125]
    LEFT JOIN [dbo].[Requester] AS [Extent126] ON ([Extent125].[Id] = [Extent126].[RegisteredAddress])
        AND ([Extent126].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent127] ON ([Extent125].[Id] = [Extent127].[ResidenceAddress])
        AND ([Extent127].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent128] ON ([Extent125].[Id] = [Extent128].[MailingAddress])
        AND ([Extent128].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent129] ON ([Extent125].[Id] = [Extent129].[CompanySeatAddress])
        AND ([Extent129].[CompanySeatAddress] IS NOT NULL)
    ) AS [Join127] ON ([Join127].[Id41] = [Extent1].[WorkplaceAddress])
    AND ([Extent1].[WorkplaceAddress] IS NOT NULL)
LEFT JOIN (
    SELECT [Extent130].[Id] AS [Id42],
        [Extent134].[Id] AS [Id23]
    FROM [dbo].[Address] AS [Extent130]
    LEFT JOIN [dbo].[Requester] AS [Extent131] ON ([Extent130].[Id] = [Extent131].[RegisteredAddress])
        AND ([Extent131].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent132] ON ([Extent130].[Id] = [Extent132].[ResidenceAddress])
        AND ([Extent132].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent133] ON ([Extent130].[Id] = [Extent133].[MailingAddress])
        AND ([Extent133].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent134] ON ([Extent130].[Id] = [Extent134].[CompanySeatAddress])
        AND ([Extent134].[CompanySeatAddress] IS NOT NULL)
    ) AS [Join132] ON ([Join132].[Id42] = [Extent1].[WorkplaceAddress])
    AND ([Extent1].[WorkplaceAddress] IS NOT NULL)
LEFT JOIN (
    SELECT [Extent135].[Id] AS [Id43]
    FROM [dbo].[Address] AS [Extent135]
    LEFT JOIN [dbo].[Requester] AS [Extent136] ON ([Extent135].[Id] = [Extent136].[RegisteredAddress])
        AND ([Extent136].[RegisteredAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent137] ON ([Extent135].[Id] = [Extent137].[ResidenceAddress])
        AND ([Extent137].[ResidenceAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent138] ON ([Extent135].[Id] = [Extent138].[MailingAddress])
        AND ([Extent138].[MailingAddress] IS NOT NULL)
    LEFT JOIN [dbo].[Requester] AS [Extent139] ON ([Extent135].[Id] = [Extent139].[CompanySeatAddress])
        AND ([Extent139].[CompanySeatAddress] IS NOT NULL)
    ) AS [Join137] ON ([Join137].[Id43] = [Extent1].[WorkplaceAddress])
    AND ([Extent1].[WorkplaceAddress] IS NOT NULL)
4

2 回答 2

1

可能WithOptionalDependent是在搞砸什么。不幸的是,我没有设置数据库来彻底测试它。

(* - 0/1)我能够测试的一个常规的、最小的关系是Person->Contact在以下设置中:

Person:
   Id
   Name
   PrimaryContactId (+ navigation property)
   SecondaryContactId (+ navigation property)

Contact:
   Id
   Description

人物图:

public class PersonMap : EntityTypeConfiguration<Person>
{
    HasKey(t => t.Id);
    Property(t => t.Name).IsRequired().HasMaxLength(30);

    ToTable("People");
    Property(t => t.Id).HasColumnName("Id");
    Property(t => t.Name).HasColumnName("Name");

    HasOptional(t => t.PrimaryContact).WithMany(t => t.PrimaryContacts)
                                      .HasForeignKey(t => t.PrimaryContactId);
    HasOptional(t => t.SecondaryContact).WithMany(t => t.SecondaryContacts)
                                        .HasForeignKey(t => t.SecondaryContactId);
}

为此:

var all = context.Set<Person>().Include(x => x.PrimaryContact).Include(x => x.SecondaryContact).ToList();

生成的 SQL 如下所示:

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[Name] AS [Name], 
[Extent2].[Id] AS [Id1], 
[Extent2].[Description] AS [Description], 
[Extent3].[Id] AS [Id2], 
[Extent3].[Description] AS [Description1]
FROM   [dbo].[People] AS [Extent1]
LEFT OUTER JOIN [dbo].[Contacts] AS [Extent2] ON [Extent1].[PrimaryContactId] = [Extent2].[Id]
LEFT OUTER JOIN [dbo].[Contacts] AS [Extent3] ON [Extent1].[SecondaryContactId] = [Extent3].[Id]

所以相当标准。

考虑到这个例子在概念上是相似的,但有不同的映射,我的赌注是在地图上。

我还让 EntityFramework Power Tools 为我从现有数据库生成 Code-First 映射... :)

于 2013-10-15T10:49:33.890 回答
0

是的,它会的。但除非它给你一个特定的性能问题,否则我会忽略它。

Entity Framework 是为不能或不想理解 SQL 的人设计的。

于 2013-10-15T10:17:29.103 回答