C# 实体框架使用外键填充错误列
我试图建立一对一的关系,但当我创建数据时,EF将外键设置为另一列。请看下面我的课程:C# 实体框架使用外键填充错误列,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,我试图建立一对一的关系,但当我创建数据时,EF将外键设置为另一列。请看下面我的课程: public class SWAChecklist { public SWAChecklist() { this.Steps = new Steps() { SWAChecklist = this }; this.Observers = new List<Observer>(); this.PersonnelObserved = ne
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; }
}
公共类检查表
{
公共卫生检查表()
{
this.Steps=newsteps(){swapchecklist=this};
this.observer=新列表();
this.personelobserved=新列表();
this.JobFactors=new JobFactors(){swapchecklist=this};
this.Causes=new Causes(){swapchecklist=this};
this.Hazards=新的Hazards(){swapchecklist=this};
}
公共int ID{get;set;}
公共字符串状态{get;set;}
公共字符串作业{get;set;}
公共原因{get;set;}
}
公共阶级事业
{
[关键]
公共int?ID{get;set;}
public int swachListId{get;set;}
[必需]
公共虚拟交换清单{get;set;}
公共布尔?附加危险{get;set;}
公共布尔?不安全行为{get;set;}
公共布尔?计划{get;set;}
}
因此,当我尝试创建SWAChecklist.Causes时,它将清单的id分配给原因上的列id,而不是SWAChecklistId。
谢谢您的帮助。公共课堂检查表
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 };
}
[Key]
public int ID { get; set; }
public string Status { get; set; }
public string Job { get; set; }
public virtual Causes Causes { get; set; }
}
public class Causes
{
[Key]
[ForeignKey("SWAChecklist")]
public int ID { get; set; }
[Required]
public bool? AdditionalHazard { get; set; }
public bool? UnsafeBehavior{ get; set; }
public bool? Planned { get; set; }
public virtual SWAChecklist SWAChecklist { get; set; }
}
{
公共卫生检查表()
{
this.Steps=newsteps(){swapchecklist=this};
this.observer=新列表();
this.personelobserved=新列表();
this.JobFactors=new JobFactors(){swapchecklist=this};
this.Causes=new Causes(){swapchecklist=this};
this.Hazards=新的Hazards(){swapchecklist=this};
}
[关键]
公共int ID{get;set;}
公共字符串状态{get;set;}
公共字符串作业{get;set;}
公共虚拟原因{get;set;}
}
公共阶级事业
{
[关键]
[外汇(“SWA清单”)]
公共int ID{get;set;}
[必需]
公共布尔?附加危险{get;set;}
公共布尔?不安全行为{get;set;}
公共布尔?计划{get;set;}
公共虚拟交换清单{get;set;}
}
您可以试试它公共类
{
公共卫生检查表()
{
this.Steps=newsteps(){swapchecklist=this};
this.observer=新列表();
this.personelobserved=新列表();
this.JobFactors=new JobFactors(){swapchecklist=this};
this.Causes=new Causes(){swapchecklist=this};
this.Hazards=新的Hazards(){swapchecklist=this};
}
[关键]
公共int ID{get;set;}
公共字符串状态{get;set;}
公共字符串作业{get;set;}
公共虚拟原因{get;set;}
}
公共阶级事业
{
[关键]
[外汇(“SWA清单”)]
公共int ID{get;set;}
[必需]
公共布尔?附加危险{get;set;}
公共布尔?不安全行为{get;set;}
公共布尔?计划{get;set;}
公共虚拟交换清单{get;set;}
}
您可以尝试一下,让我们假设EF允许您与从属端的
ID
和swacheListId
建立1:0..1关系。为了避免1:many关系,EF需要对swacheListId
进行唯一约束。然后您将拥有swacheListId
,一个唯一的(外来)键列-这是对主键的完美描述
因此,对于1:0..1关系,使用同一列作为主键和外键是一个很好的设计决策,EF遵循它
如果您(出于任何原因)需要一个单独的外键,您可以配置一个1:many关系,并在外键上创建一个唯一的索引,以有效地将“many”限制为0..1让我们假设EF将允许您与依赖侧的
ID
和swacheListId
建立1:0..1关系。为了避免1:many关系,EF需要对swacheListId
进行唯一约束。然后您将拥有swacheListId
,一个唯一的(外来)键列-这是对主键的完美描述
因此,对于1:0..1关系,使用同一列作为主键和外键是一个很好的设计决策,EF遵循它
如果(出于任何原因)需要单独的外键,可以配置1:many关系并在外键上创建唯一索引,以有效地将“many”限制为0..1