私は、エンティティフレームワークで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;
}
}
誰もがこのことについてどんな考えを持っていますか?
TimerowOvertimesにデータがありますか? –
これを試してください: 'var timeRows = db.Timerows.Include(" TimerowOvertimes ")。ToList();' –
あなたのDbContextで遅延ロードをオフにしましたか? – uk2k05