Using Entity Framework.
Simple Page class with tree hierarchy.
public class Page
{
public int Id { get; set; }
//...
public int ParentId { get; set; } //removing this solves the problem, but I would like to keep this line
public virtual Page Parent { get; set; }
public virtual ICollection<Page> SubPages { get; set; }
}
Throws an error when I’m trying to add an object and SaveChanges:
Unable to determine the principal end of the ‘Models.Page_Parent’ relationship. Multiple added entities may have the same primary key.
I roughly understand the problem, but have no idea how to fix it.
Make the
ParentIdproperty nullable. Root element won’t have a parent. Use the fluent configuration as shown in @Jayantha’s answer.