这是一对多的模型(申请人)。 (AttachedDocument)是很多模型,我认为我的关系有误
public partial class Applicant
{
[Key]
public int ApplicantID { get; set; }
public string ApplicationSource { get; set; }
public string Gender { get; set; }
[ForeignKey("ApplicantID")]
public virtual ICollection<AttachedDocuments> AttachedDocuments { get; set; }
}
public partial class AttachedDocuments
{
[Key]
public int AttachedDocID { get; set; }
public int ApplicantID { get; set; }
public string AttechedDocGUID { get; set; }
public int DocumentTypeID { get; set; }
[ForeignKey("ApplicantID")]
public virtual ICollection<Applicant> Applicant { get; set; }
}
这里是构建器:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Applicant>().HasRequired(a => a.AttachedDocuments).WithMany().HasForeignKey(a => a.ApplicantID);
modelBuilder.Entity<AttachedDocuments>().HasOptional(b => b.Applicant).WithMany().HasForeignKey(b => b.ApplicantID);
}
第一次建立具有关系的模型。预先感谢!
答案 0 :(得分:1)
您是否尝试过从申请人的类别中删除外键属性? ForeignKey属性指定将作为外键的属性的名称。您正在指定Key属性的名称,这将导致数据库翻转。
public partial class Applicant
{
[Key]
public int ApplicantID { get; set; }
public string ApplicationSource { get; set; }
public string Gender { get; set; }
public virtual ICollection<AttachedDocuments> AttachedDocuments { get; set; }
}
public partial class AttachedDocuments
{
[Key]
public int AttachedDocID { get; set; }
public string AttachedDocGUID { get; set; }
public int DocumentTypeID { get; set; }
public int ApplicantID { get; set; }
[ForeignKey("ApplicantID")]
public virtual Applicant Applicant { get; set; }
}