我知道有相关话题:two Foreign Keys from same table,但我无法找到解决问题的方法。我对EF很新。
我有以下模型类(Code-First):
public class Member
{
[Key]
public int MemberID { get; set; }
public string Name {get; set;}
public string Surname { get; set; }
public virtual ICollection<Marriage> Marriages { get; set; }
}
public class Marriage
{
[Key]
public int MarriageID { get; set; }
public string MarriagePlace { get; set; }
public DateTime MarriageDate { get; set; }
[ForeignKey("Husband")]
public int HusbandID { get; set; }
[ForeignKey("Wife")]
public int WifeID { get; set; }
public virtual Member Husband { get; set; }
public virtual Member Wife { get; set; }
}
我的问题是,丈夫和妻子都应该与会员班级中的同一个婚姻系列相关联。我做到了:
modelBuilder.Entity<Marriage>()
.HasRequired<Member>(m => m.Husband)
.WithMany(m => m.Marriages)
.HasForeignKey(m => m.HusbandID)
.WillCascadeOnDelete(false);
丈夫现在与Merriages系列相关联。但是当我试图为妻子财产添加相同的东西时,一切都会破裂:
modelBuilder.Entity<Marriage>()
.HasRequired<Member>(m => m.Wife)
.WithMany(m => m.Marriages)
.HasForeignKey(m => m.WifeID)
.WillCascadeOnDelete(false);
我收到了一个错误:
错误1指定的架构无效。错误:未加载“FamilyTree.Models.Marriage_Husband”关系,因为“FamilyTree.Models.Member”类型不可用。 C:\ Users \ Sumiteru \ Documents \ Visual Studio 2013 \ Projects \ FamilyTree \ FamilyTree \ App.xaml 9 21 FamilyTree