0

可能是什么问题?results.SubAreas始终为空。我整个下午都在审查我所有的数据库约束、代码、pocos、映射、数据注释。results.ToString() 查询显示它正在加入子区域,当我在 Management Studio 中运行查询时,它会返回行。但它看起来很奇怪。查询后,我已经包含了 poco 定义。

询问

var results = (from b in _context.Buildings
               .Include("SubAreas")
               .Include("ExtraFeatures")
               .Include("StructuralElements")
               where b.STRAP == strap && b.NUM == bld_num
               select b).FirstOrDefault();

SQL 语句

SELECT 
[UnionAll2].[C2] AS [C1], 
[UnionAll2].[C3] AS [C2], 
[UnionAll2].[C4] AS [C3], 
[UnionAll2].[C5] AS [C4], 
[UnionAll2].[C6] AS [C5], 
[UnionAll2].[C7] AS [C6], 
[UnionAll2].[C8] AS [C7], 
[UnionAll2].[C9] AS [C8], 
[UnionAll2].[C10] AS [C9], 
[UnionAll2].[C1] AS [C10], 
[UnionAll2].[C11] AS [C11], 
[UnionAll2].[C12] AS [C12], 
[UnionAll2].[C13] AS [C13], 
[UnionAll2].[C14] AS [C14], 
[UnionAll2].[C15] AS [C15], 
[UnionAll2].[C16] AS [C16], 
[UnionAll2].[C17] AS [C17], 
[UnionAll2].[C18] AS [C18], 
[UnionAll2].[C19] AS [C19], 
[UnionAll2].[C20] AS [C20], 
[UnionAll2].[C21] AS [C21], 
[UnionAll2].[C22] AS [C22], 
[UnionAll2].[C23] AS [C23], 
[UnionAll2].[C24] AS [C24], 
[UnionAll2].[C25] AS [C25], 
[UnionAll2].[C26] AS [C26], 
[UnionAll2].[C27] AS [C27], 
[UnionAll2].[C28] AS [C28], 
[UnionAll2].[C29] AS [C29], 
[UnionAll2].[C30] AS [C30], 
[UnionAll2].[C31] AS [C31], 
[UnionAll2].[C32] AS [C32], 
[UnionAll2].[C33] AS [C33], 
[UnionAll2].[C34] AS [C34], 
[UnionAll2].[C35] AS [C35]
FROM  (SELECT 
    [UnionAll1].[C1] AS [C1], 
    [UnionAll1].[NUM] AS [C2], 
    [UnionAll1].[STRAP] AS [C3], 
    [UnionAll1].[NUM1] AS [C4], 
    [UnionAll1].[SITUS] AS [C5], 
    [UnionAll1].[TYPE] AS [C6], 
    [UnionAll1].[STYLE] AS [C7], 
    [UnionAll1].[YEAR_BUILT] AS [C8], 
    [UnionAll1].[GROSS_AREA] AS [C9], 
    [UnionAll1].[LIVING_AREA] AS [C10], 
    [UnionAll1].[STRAP1] AS [C11], 
    [UnionAll1].[BUILDING_NUM] AS [C12], 
    [UnionAll1].[SAR_LN_NUM] AS [C13], 
    [UnionAll1].[DSCR] AS [C14], 
    [UnionAll1].[GROSS_AREA1] AS [C15], 
    [UnionAll1].[VALUE] AS [C16], 
    [UnionAll1].[STRAP2] AS [C17], 
    [UnionAll1].[BUILDING_NUM1] AS [C18], 
    [UnionAll1].[C2] AS [C19], 
    [UnionAll1].[C3] AS [C20], 
    [UnionAll1].[C4] AS [C21], 
    [UnionAll1].[C5] AS [C22], 
    [UnionAll1].[C6] AS [C23], 
    [UnionAll1].[C7] AS [C24], 
    [UnionAll1].[C8] AS [C25], 
    [UnionAll1].[C9] AS [C26], 
    [UnionAll1].[C10] AS [C27], 
    [UnionAll1].[C11] AS [C28], 
    [UnionAll1].[C12] AS [C29], 
    [UnionAll1].[C13] AS [C30], 
    [UnionAll1].[C14] AS [C31], 
    [UnionAll1].[C15] AS [C32], 
    [UnionAll1].[C16] AS [C33], 
    [UnionAll1].[C17] AS [C34], 
    [UnionAll1].[C18] AS [C35]
    FROM  (SELECT 
        CASE WHEN ([Extent2].[BUILDING_NUM] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1], 
        [Extent1].[NUM] AS [NUM], 
        [Extent1].[STRAP] AS [STRAP], 
        [Extent1].[NUM] AS [NUM1], 
        [Extent1].[SITUS] AS [SITUS], 
        [Extent1].[TYPE] AS [TYPE], 
        [Extent1].[STYLE] AS [STYLE], 
        [Extent1].[YEAR_BUILT] AS [YEAR_BUILT], 
        [Extent1].[GROSS_AREA] AS [GROSS_AREA], 
        [Extent1].[LIVING_AREA] AS [LIVING_AREA], 
        [Extent2].[STRAP] AS [STRAP1], 
        [Extent2].[BUILDING_NUM] AS [BUILDING_NUM], 
        [Extent2].[SAR_LN_NUM] AS [SAR_LN_NUM], 
        [Extent2].[DSCR] AS [DSCR], 
        [Extent2].[GROSS_AREA] AS [GROSS_AREA1], 
        [Extent2].[VALUE] AS [VALUE], 
        [Extent2].[STRAP] AS [STRAP2], 
        [Extent2].[BUILDING_NUM] AS [BUILDING_NUM1], 
        CAST(NULL AS varchar(1)) AS [C2], 
        CAST(NULL AS int) AS [C3], 
        CAST(NULL AS int) AS [C4], 
        CAST(NULL AS varchar(1)) AS [C5], 
        CAST(NULL AS decimal(18,2)) AS [C6], 
        CAST(NULL AS decimal(18,2)) AS [C7], 
        CAST(NULL AS int) AS [C8], 
        CAST(NULL AS varchar(1)) AS [C9], 
        CAST(NULL AS varchar(1)) AS [C10], 
        CAST(NULL AS varchar(1)) AS [C11], 
        CAST(NULL AS int) AS [C12], 
        CAST(NULL AS int) AS [C13], 
        CAST(NULL AS varchar(1)) AS [C14], 
        CAST(NULL AS varchar(1)) AS [C15], 
        CAST(NULL AS varchar(1)) AS [C16], 
        CAST(NULL AS varchar(1)) AS [C17], 
        CAST(NULL AS int) AS [C18]
        FROM  [dbo].[Buildings] AS [Extent1]
        LEFT OUTER JOIN [dbo].[SUB_AREAS] AS [Extent2] ON ([Extent1].[STRAP] = [Extent2].[STRAP]) AND ([Extent1].[NUM] = [Extent2].[BUILDING_NUM])
        WHERE ([Extent1].[STRAP] = '0001020003') AND ([Extent1].[NUM] = 1)
    UNION ALL
        SELECT 
        2 AS [C1], 
        [Extent3].[NUM] AS [NUM], 
        [Extent3].[STRAP] AS [STRAP], 
        [Extent3].[NUM] AS [NUM1], 
        [Extent3].[SITUS] AS [SITUS], 
        [Extent3].[TYPE] AS [TYPE], 
        [Extent3].[STYLE] AS [STYLE], 
        [Extent3].[YEAR_BUILT] AS [YEAR_BUILT], 
        [Extent3].[GROSS_AREA] AS [GROSS_AREA], 
        [Extent3].[LIVING_AREA] AS [LIVING_AREA], 
        CAST(NULL AS varchar(1)) AS [C2], 
        CAST(NULL AS int) AS [C3], 
        CAST(NULL AS int) AS [C4], 
        CAST(NULL AS varchar(1)) AS [C5], 
        CAST(NULL AS int) AS [C6], 
        CAST(NULL AS int) AS [C7], 
        CAST(NULL AS varchar(1)) AS [C8], 
        CAST(NULL AS int) AS [C9], 
        [Extent4].[STRAP] AS [STRAP1], 
        [Extent4].[LN_NUM] AS [LN_NUM], 
        [Extent4].[BUILDING_NUM] AS [BUILDING_NUM], 
        [Extent4].[DSCR] AS [DSCR], 
        [Extent4].[UNITS] AS [UNITS], 
        [Extent4].[UNIT_PRICE] AS [UNIT_PRICE], 
        [Extent4].[YEAR_BUILT] AS [YEAR_BUILT1], 
        [Extent4].[CD] AS [CD], 
        [Extent4].[STRAP] AS [STRAP2], 
        CAST(NULL AS varchar(1)) AS [C10], 
        CAST(NULL AS int) AS [C11], 
        CAST(NULL AS int) AS [C12], 
        CAST(NULL AS varchar(1)) AS [C13], 
        CAST(NULL AS varchar(1)) AS [C14], 
        CAST(NULL AS varchar(1)) AS [C15], 
        CAST(NULL AS varchar(1)) AS [C16], 
        CAST(NULL AS int) AS [C17]
        FROM  [dbo].[Buildings] AS [Extent3]
        INNER JOIN [dbo].[EXTRA_FEATURES] AS [Extent4] ON ([Extent3].[STRAP] = [Extent4].[STRAP]) AND ([Extent3].[NUM] = [Extent4].[BUILDING_NUM])
        WHERE ([Extent3].[STRAP] = '0001020003') AND ([Extent3].[NUM] = 1)) AS [UnionAll1]
UNION ALL
    SELECT 
    3 AS [C1], 
    [Extent5].[NUM] AS [NUM], 
    [Extent5].[STRAP] AS [STRAP], 
    [Extent5].[NUM] AS [NUM1], 
    [Extent5].[SITUS] AS [SITUS], 
    [Extent5].[TYPE] AS [TYPE], 
    [Extent5].[STYLE] AS [STYLE], 
    [Extent5].[YEAR_BUILT] AS [YEAR_BUILT], 
    [Extent5].[GROSS_AREA] AS [GROSS_AREA], 
    [Extent5].[LIVING_AREA] AS [LIVING_AREA], 
    CAST(NULL AS varchar(1)) AS [C2], 
    CAST(NULL AS int) AS [C3], 
    CAST(NULL AS int) AS [C4], 
    CAST(NULL AS varchar(1)) AS [C5], 
    CAST(NULL AS int) AS [C6], 
    CAST(NULL AS int) AS [C7], 
    CAST(NULL AS varchar(1)) AS [C8], 
    CAST(NULL AS int) AS [C9], 
    CAST(NULL AS varchar(1)) AS [C10], 
    CAST(NULL AS int) AS [C11], 
    CAST(NULL AS int) AS [C12], 
    CAST(NULL AS varchar(1)) AS [C13], 
    CAST(NULL AS decimal(18,2)) AS [C14], 
    CAST(NULL AS decimal(18,2)) AS [C15], 
    CAST(NULL AS int) AS [C16], 
    CAST(NULL AS varchar(1)) AS [C17], 
    CAST(NULL AS varchar(1)) AS [C18], 
    [Extent6].[STRAP] AS [STRAP1], 
    [Extent6].[BUILDING_NUM] AS [BUILDING_NUM], 
    [Extent6].[LN_NUM] AS [LN_NUM], 
    [Extent6].[CATEGORY] AS [CATEGORY], 
    [Extent6].[DSCR] AS [DSCR], 
    [Extent6].[VALUE] AS [VALUE], 
    [Extent6].[STRAP] AS [STRAP2], 
    [Extent6].[BUILDING_NUM] AS [BUILDING_NUM1]
    FROM  [dbo].[Buildings] AS [Extent5]
    INNER JOIN [dbo].[STRUCTURAL_ELEMENTS] AS [Extent6] ON ([Extent5].[STRAP] = [Extent6].[STRAP]) AND ([Extent5].[NUM] = [Extent6].[BUILDING_NUM])
    WHERE ([Extent5].[STRAP] = '0001020003') AND ([Extent5].[NUM] = 1)) AS [UnionAll2]
ORDER BY [UnionAll2].[C3] ASC, [UnionAll2].[C4] ASC, [UnionAll2].[C1] ASC

模型类

[Table("STRUCTURAL_ELEMENTS")]
public class StructuralElement
{
    [Key]
    [Column(Order = 0)]
    public string STRAP { get; set; }
    [Key]
    [Column(Order = 1)]
    public int BUILDING_NUM { get; set; }
    [Key]
    [Column(Order = 2)]
    public int LN_NUM { get; set; }
    [Key]
    [Column(Order = 3)]
    public string CATEGORY { get; set; }
    public string DSCR { get; set; }
    public string VALUE { get; set; }

}

[Table("SUB_AREAS")]
public class SubArea
{
    [Key]
    [Column(Order = 0)]
    public string STRAP { get; set; }
    [Key]
    [Column(Order = 1)]
    public int BUILDING_NUM { get; set; }
    [Key]
    [Column(Order = 2)]
    public int SAR_LN_NUM { get; set; }

    public string DSCR { get; set; }
    public int GROSS_AREA { get; set; }
    public int VALUE { get; set; }
}

[Table("EXTRA_FEATURES")]
public class ExtraFeature
{
    [Key]
    [Column(Order = 0)]
    public string STRAP { get; set; }
    [Column(Order = 1)]
    public int BUILDING_NUM { get; set; }
    [Key]
    [Column(Order = 2)]
    public int LN_NUM { get; set; }

    public string DSCR { get; set; }
    public decimal? UNITS { get; set; }
    public decimal? UNIT_PRICE { get; set; }
    public int? YEAR_BUILT { get; set; }
    public string CD { get; set; }

}

public class Building
{
    [Key]
    [Column(Order = 0)]
    public string STRAP { get; set; }

    [Key]
    [Column(Order = 1)]
    public int NUM { get; set; }
    public string SITUS { get; set; }
    public string TYPE { get; set; }
    public string STYLE { get; set; }
    //public string CLASS { get; set; }
    public int? YEAR_BUILT { get; set; }
    public int? GROSS_AREA { get; set; }
    public int? LIVING_AREA { get; set; }

    [ForeignKey("STRAP,BUILDING_NUM")]
    public  ICollection<StructuralElement> StructuralElements { get; set; }

    [ForeignKey("STRAP,BUILDING_NUM")]
    public  ICollection<ExtraFeature> ExtraFeatures { get; set; }

    [ForeignKey("STRAP,BUILDING_NUM")]
    public  ICollection<SubArea> SubAreas { get; set; }


}
4

2 回答 2

0

我在空表中的键是用空格存储的,所以 EF 没有将它们链接到它们的父级。

于 2013-04-09T18:49:21.337 回答
0

它看起来像是一种优化。您正在尝试使用相关集合加载 Building。现在假设建筑物有 100 个子区域、100 个额外功能和 100 个结构元素,那么简单的 JOIN 会产生什么?正确,一百万行。ORM 并不像通常认为的那样愚蠢,使用 UNION 查询将返回 301 行。

于 2013-03-19T14:50:34.437 回答