2016-06-17 3 views
0

私は、エンティティフレームワークで0から1つの関係を持つコンテキストを持っています。 私のエンティティを読み込むときに、関連するエンティティは読み込まれません。それは常にnullです。1から0まで、または1つのEntityframeworkナビゲーションプロパティnull

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; } 
} 

しかし、私はTimerowと関連timerowOvertimesを選択しようとすると、timerowOvertimesはオールウェイズnullに:

は、ここに私のクラスです。

私はこれを試してみてください。

var timeRows = db.Timerows.ToList(); 

    foreach (var timeRow in timeRows) 
       { 
    TimeSheet modelRow = new TimeSheet 
        { 
         Id = timeRow.Id, 
         Date = timeRow.Date, 
         BestNo = timeRow.BestNo, 
         PosNo = timeRow.PosNo, 
         Comment = timeRow.Comment, 
         Hours = timeRow.Hours, 
         Ready = timeRow.Ready, 
         SkillsNoId = timeRow.SkillsNoId 
        }; 

    if(timeRow.TimerowOvertimes == null) 
        { 
         modelRow.ATF = false; 
         modelRow.Weekend = false; 
         modelRow.Overtime = 0; 
        } 
        else 
        { 
         modelRow.Overtime = timeRow.TimerowOvertimes.Hours; 
         modelRow.ATF = timeRow.TimerowOvertimes.ATF; 
         modelRow.Weekend = timeRow.TimerowOvertimes.Weekend; 
        } 
} 

誰もがこのことについてどんな考えを持っていますか?

+0

TimerowOvertimesにデータがありますか? –

+0

これを試してください: 'var timeRows = db.Timerows.Include(" TimerowOvertimes ")。ToList();' –

+0

あなたのDbContextで遅延ロードをオフにしましたか? – uk2k05

答えて

0

あなたは両方[Key][ForeignKey]として注釈TimerowOvertimeId性質を持つべきではありません。現在はTimerowOvertimeエンティティの主キーを使用してTimerowをバインドするように見えます。したがって、TimerowOverTimeエンティティを拡張してTimerowIdプロパティを含むようにし、[ForeignKey]属性で注釈を付ける必要があります。

 [ForeignKey("Timerow")] 
     public int TimerowId { get; set; } 
+0

それは、感謝の事でした。 –

関連する問題