我試圖建立一對一的關係,但是當我創建數據EF設置外鍵作爲另一列。請參閱我下面的類:實體框架與外鍵填充錯誤的列
public class SWAChecklist
{
public SWAChecklist()
{
this.Steps = new Steps() { SWAChecklist = this };
this.Observers = new List<Observer>();
this.PersonnelObserved = new List<PersonObserved>();
this.JobFactors = new JobFactors() { SWAChecklist = this };
this.Causes = new Causes() { SWAChecklist = this };
this.Hazards = new Hazards() { SWAChecklist = this };
}
public int ID { get; set; }
public string Status { get; set; }
public string Job { get; set; }
public Causes Causes { get; set; }
}
public class Causes
{
[Key]
public int? ID { get; set; }
public int SWAChecklistId { get; set; }
[Required]
public virtual SWAChecklist SWAChecklist { get; set; }
public bool? AdditionalHazard { get; set; }
public bool? UnsafeBehavior{ get; set; }
public bool? Planned { get; set; }
}
所以,當我嘗試創建一個SWAChecklist.Causes,它被賦予的,而不是SWAChecklistId清單上的原因列ID的ID。 感謝您的幫助。
謝謝男人,這很有道理。 :) –