创建可选的多对多映射实体框架6.1代码优先

时间:2014-08-27 05:27:36

标签: entity-framework ef-code-first many-to-many

我正在尝试使用EF 6.1 Code First设置可选的多对多关系。我有一个可以拥有0个或更多基金组的合作伙伴。我已使用以下代码成功创建了映射:

HasMany(t => t.FundGroups)
            .WithMany()
            .Map(x =>
            {
                x.MapLeftKey("PartnerId");
                x.MapRightKey("FundGroupId");
                x.ToTable("PartnerFundGroupMap", "admin");
            })
            .MapToStoredProcedures(s => s.Insert(i =>    i.HasName("admin.InsertPartnerFundGroups")
                .LeftKeyParameter(p => p.PartnerId, "PartnerId")
                .RightKeyParameter(p => p.FundGroupId, "FundGroupId"))
                .Delete(i => i.HasName("admin.DeletePartnerFundGroups")
                .LeftKeyParameter(p => p.PartnerId, "PartnerId")
                .RightKeyParameter(p => p.FundGroupId, "FundGroupId")));

问题是代码创建了一个内部联接,我没有找回没有关联基金组的实体。有没有办法强制左连接,所以我总是可以检索合作伙伴,即使基金组没有与之关联。请参阅下面生成的查询:

SELECT 
[Project1].[IRSEntityTypeID1] AS [IRSEntityTypeID], 
[Project1].[RegionID1] AS [RegionID], 
[Project1].[CountryID1] AS [CountryID], 
[Project1].[PartnershipLevelId1] AS [PartnershipLevelId], 
[Project1].[PartnerID] AS [PartnerID], 
[Project1].[C1] AS [C1], 
[Project1].[ExternalCode] AS [ExternalCode], 
[Project1].[CompanyCode] AS [CompanyCode], 
[Project1].[PartnerName] AS [PartnerName], 
[Project1].[EIN] AS [EIN], 
[Project1].[KitCode] AS [KitCode], 
[Project1].[IRSEntityTypeID] AS [IRSEntityTypeID1], 
[Project1].[Address1] AS [Address1], 
[Project1].[Address2] AS [Address2], 
[Project1].[City] AS [City], 
[Project1].[IsPartnership] AS [IsPartnership], 
[Project1].[SessionId] AS [SessionId], 
[Project1].[RegionID] AS [RegionID1], 
[Project1].[CountryID] AS [CountryID1], 
[Project1].[PostalCode] AS [PostalCode], 
[Project1].[PartnershipLevelId] AS [PartnershipLevelId1], 
[Project1].[C2] AS [C2], 
[Project1].[C4] AS [C3], 
[Project1].[C5] AS [C4], 
[Project1].[Description] AS [Description], 
[Project1].[C6] AS [C5], 
[Project1].[C7] AS [C6], 
[Project1].[CountryName] AS [CountryName], 
[Project1].[C8] AS [C7], 
[Project1].[C9] AS [C8], 
[Project1].[RegionName] AS [RegionName], 
[Project1].[C10] AS [C9], 
[Project1].[C11] AS [C10], 
[Project1].[IRSEntityTypeName] AS [IRSEntityTypeName], 
[Project1].[C12] AS [C11], 
[Project1].[C13] AS [C12], 
[Project1].[C14] AS [C13], 
[Project1].[C15] AS [C14], 
[Project1].[C16] AS [C15], 
[Project1].[FundGroupID] AS [FundGroupID], 
[Project1].[C3] AS [C16], 
[Project1].[FundGroupCode] AS [FundGroupCode], 
[Project1].[FundGroupName] AS [FundGroupName], 
[Project1].[SessionId1] AS [SessionId1], 
[Project1].[CreateDate] AS [CreateDate], 
[Project1].[ModifiedDate] AS [ModifiedDate]
FROM ( SELECT 
    [Extent1].[PartnerID] AS [PartnerID], 
    [Extent1].[ExternalCode] AS [ExternalCode], 
    [Extent1].[CompanyCode] AS [CompanyCode], 
    [Extent1].[PartnerName] AS [PartnerName], 
    [Extent1].[EIN] AS [EIN], 
    [Extent1].[KitCode] AS [KitCode], 
    [Extent1].[IRSEntityTypeID] AS [IRSEntityTypeID], 
    [Extent1].[Address1] AS [Address1], 
    [Extent1].[Address2] AS [Address2], 
    [Extent1].[City] AS [City], 
    [Extent1].[IsPartnership] AS [IsPartnership], 
    [Extent1].[SessionId] AS [SessionId], 
    [Extent1].[RegionID] AS [RegionID], 
    [Extent1].[CountryID] AS [CountryID], 
    [Extent1].[PostalCode] AS [PostalCode], 
    [Extent1].[PartnershipLevelId] AS [PartnershipLevelId], 
    [Extent2].[PartnershipLevelId] AS [PartnershipLevelId1], 
    [Extent2].[Description] AS [Description], 
    [Extent3].[CountryID] AS [CountryID1], 
    [Extent3].[CountryName] AS [CountryName], 
    [Extent4].[RegionID] AS [RegionID1], 
    [Extent4].[RegionName] AS [RegionName], 
    [Extent5].[IRSEntityTypeID] AS [IRSEntityTypeID1], 
    [Extent5].[IRSEntityTypeName] AS [IRSEntityTypeName], 
    N''3ed69e78-cbfb-4d5b-a270-85d0d62bb11c'' AS [C1], 
    N''FundGroups'' AS [C2], 
    [Join5].[FundGroupID1] AS [FundGroupID], 
    [Join5].[FundGroupCode] AS [FundGroupCode], 
    [Join5].[FundGroupName] AS [FundGroupName], 
    [Join5].[SessionID] AS [SessionId1], 
    [Join5].[CreateDate] AS [CreateDate], 
    [Join5].[ModifiedDate] AS [ModifiedDate], 
    CASE WHEN ([Join5].[PartnerId] IS NULL) THEN CAST(NULL AS varchar(1)) ELSE N''3ed69e78-cbfb-4d5b-a270-85d0d62bb11c'' END AS [C3], 
    N''PartnershipLevel'' AS [C4], 
    N''3ed69e78-cbfb-4d5b-a270-85d0d62bb11c'' AS [C5], 
    N''Country'' AS [C6], 
    N''3ed69e78-cbfb-4d5b-a270-85d0d62bb11c'' AS [C7], 
    N''Region'' AS [C8], 
    N''3ed69e78-cbfb-4d5b-a270-85d0d62bb11c'' AS [C9], 
    N''IRSEntityType'' AS [C10], 
    N''3ed69e78-cbfb-4d5b-a270-85d0d62bb11c'' AS [C11], 
    cast(0 as bit) AS [C12], 
    cast(0 as bit) AS [C13], 
    cast(0 as bit) AS [C14], 
    cast(0 as bit) AS [C15], 
    CASE WHEN ([Join5].[PartnerId] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C16]
    FROM      [admin].[vPartner] AS [Extent1]
    INNER JOIN [admin].[PartnershipLevel] AS [Extent2] ON [Extent1].[PartnershipLevelId] = [Extent2].[PartnershipLevelId]
    INNER JOIN [admin].[Country] AS [Extent3] ON [Extent1].[CountryID] = [Extent3].[CountryID]
    INNER JOIN [admin].[Region] AS [Extent4] ON [Extent1].[RegionID] = [Extent4].[RegionID]
    INNER JOIN [admin].[IRSEntityType] AS [Extent5] ON [Extent1].[IRSEntityTypeID] = [Extent5].[IRSEntityTypeID]
    LEFT OUTER JOIN  (SELECT [Extent6].[PartnerId] AS [PartnerId], [Extent7].[FundGroupID] AS [FundGroupID1], [Extent7].[FundGroupCode] AS [FundGroupCode], [Extent7].[FundGroupName] AS [FundGroupName], [Extent7].[SessionID] AS [SessionID], [Extent7].[CreateDate] AS [CreateDate], [Extent7].[ModifiedDate] AS [ModifiedDate]
        FROM  [admin].[PartnerFundGroupMap] AS [Extent6]
        INNER JOIN [admin].[FundGroup] AS [Extent7] ON [Extent6].[FundGroupId] = [Extent7].[FundGroupID] ) AS [Join5] ON [Extent1].[PartnerID] = [Join5].[PartnerId]
    WHERE [Extent1].[PartnerID] = @p__linq__0
)  AS [Project1]
ORDER BY [Project1].[IRSEntityTypeID1] ASC, [Project1].[RegionID1] ASC, [Project1].[CountryID1] ASC, [Project1].[PartnershipLevelId1] ASC, [Project1].[PartnerID] ASC, [Project1].[C16] ASC',N'@p__linq__0 int',@p__linq__0=1

1 个答案:

答案 0 :(得分:0)

我找到了这个问题。它与“多对多”映射无关,也是另一个实体所需映射的结果。感谢Dabblernl让我发布查询,在进一步检查查询后我发现了问题