一到零或一个Entityframework导航属性为null

时间:2016-06-17 09:05:06

标签: c# entity-framework

我在实体框架中有一个到零或一个关系的上下文。 但是当我加载我的实体时,它不会加载相关实体。它总是无效。

这是我的课程:

td

但是当我尝试选择Timerow和相关的timerowOvertimes时,timerowOvertimes总是为空。

我试试这个:

public class Timerow
    {
            [Key]
            public int Id { get; set; }

            [Required]
            public int BestNo { get; set; }

            [Required]
            public int PosNo { get; set; }

            [Required]
            public int EmpNo { get; set; }
            public virtual TimerowOvertime TimerowOvertimes { get; set; }
}

public class TimerowOvertime
    {
            [Key]
            [ForeignKey("Timerow")]
            public int Id { get; set; }
            [Required]
            public float Hours { get; set; }
            public DateTime? Transfered { get; set; }
            public bool Weekend { get; set; }
            public bool ATF { get; set; }
            [Required]
            public virtual Timerow Timerow { get; set; }
}

有人对此有任何想法吗?

1 个答案:

答案 0 :(得分:0)

您不应将24391 04ZZ 13ZZ 22ZZ 31ZZ 40ZZ 05YZ 14YZ 23YZ 32YZ 41YZ Id属性注释为TimerowOvertime[Key]。目前,它希望使用[ForeignKey]实体的主键绑定Timerow。因此,您需要扩展TimerowOvertime实体以包含TimerowOverTime属性,并使用TimerowId属性对其进行注释。

[ForeignKey]
相关问题