I know that there is related topic: two Foreign Keys from same table, but I can't find there fix to my problem. I am pretty new to EF.
I have the following model classes (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; }
}
My problem is that both Husband and Wife should be connected to the same Marriage collection in Member class. I did that:
modelBuilder.Entity<Marriage>()
.HasRequired<Member>(m => m.Husband)
.WithMany(m => m.Marriages)
.HasForeignKey(m => m.HusbandID)
.WillCascadeOnDelete(false);
And husband is now connected to Merriages collection. But everything breaks when I'm trying to add the same thing for Wife property:
modelBuilder.Entity<Marriage>()
.HasRequired<Member>(m => m.Wife)
.WithMany(m => m.Marriages)
.HasForeignKey(m => m.WifeID)
.WillCascadeOnDelete(false);
and I am getting an error:
Error 1 Schema specified is not valid. Errors: The relationship 'FamilyTree.Models.Marriage_Husband' was not loaded because the type 'FamilyTree.Models.Member' is not available. C:\Users\Sumiteru\Documents\Visual Studio 2013\Projects\FamilyTree\FamilyTree\App.xaml 9 21 FamilyTree