I have a class, like so
public MyObject {
public long Id { get; set; }
public string Name {get; set; }
}
I would like to setup a link between this class and another object of the same type. So i need another object to handle the many-to-many relationship, which is fine, something like this
public MyRelationship {
public long Id { get; set; }
public string Name { get; set; }
}
But how do i configure the rest. I have tried examples online, but they are too different objects.
I have tried adding to navigation properties to MyRelationship for the link, like so
public MyRelationship {
public long Id { get; set; }
public string Name { get; set; }
public MyObject Parent { get; set; }
public MyObject Child { get; set; }
}
And also 2 collections to MyObject, like so
public MyObject {
public long Id { get; set; }
public string Name {get; set; }
public virtual ICollection<MyRelationship> ParentRelationships { get; set; }
public virtual ICollection<MyRelationship> ChildRelationships { get; set; }
}
Then in my configuration file i have added
// relationships
this.HasMany(e => e.ParentRelationships)
.WithRequired(e => e.Parent)
.HasForeignKey(e => e.ParentId)
.WillCascadeOnDelete(false);
this.HasMany(e => e.ChildRelationships)
.WithRequired(e => e.Child)
.HasForeignKey(e => e.ChildId)
.WillCascadeOnDelete(false);
But i get errors, like this
he navigation property 'ChildRelationships' declared on type 'MyObject' has been configured with conflicting foreign keys.