When I run a migration I get the following error:
The given key 'System.Nullable1[System.Int32] ChildId' was not present in the dictionary.
Basically I want to create a relationship for a contact with another contact, e.g. Contact1 has a relationship with Contact2 of type Employee. One Contact can have many relationships with other Contacts. There is only one relationship type for each relationship. So under Contact1 you may see 3 relationship recrods, e.g. Contact2;Supervisor, Contact2;Friend, Contact3; Dentist.
My navigation properties are wrong, but I can't figure out how to set this up. Any help would be greatly appreciated :)
Contact Model
public class Contact
{
public int Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
[InverseProperty(nameof(Relationship.ParentId))]
public virtual ICollection<Relationship> Parent { get; set; }
[InverseProperty(nameof(Relationship.ChildId))]
public virtual ICollection<Relationship> Child { get; set; }
}
Relationship Model
public class Relationship
{
public int Id { get; set; }
[ForeignKey(nameof(Parent)), Column(Order = 0)]
public int? ParentId { get; set; }
public virtual Contact? Parent { get; set; }
[ForeignKey(nameof(Child)), Column(Order = 1)]
public int? ChildId { get; set; }
public virtual Contact? Child { get; set; }
// Navigation for the relationship type
public int? RelationshipId { get; set; }
public virtual Relationship? Relationships { get; set; }
}