Note that there are some explanatory texts on larger screens.

plurals
  1. POassociation of two entities through a third one in .NET MVC 4
    primarykey
    data
    text
    <p>I am building a website using .NET MVC 4. What I have is Persons that have roles in regard to other Persons. For example A is Father to B,D and A is Husband to C.</p> <p>I am not sure if this is the right way to do it but I was thinking of having a class Persons and a class Roles. Somehow I want to connect an instance of Persons with another instance of Persons through an instance of Roles and save that in the database.</p> <p>So the association table will have to have three columns Person1|Role|Person2.</p> <p>How can I best represent this in a model?</p> <p>EDIT</p> <p>Thanks for the help. I did something pretty similar to what you proposed.</p> <pre><code>public class Persons { public Guid PersonsID { get; set; } public string Name { get; set; } ICollection&lt;Relations&gt; Relations { get; set; } } public class RelationshipType { public Guid ID {get; set;} public string Type { get; set; } public ICollection&lt;Relations&gt; Relations { get; set; } } public class Relations { public Guid RelationsID { get; set; } public virtual Persons RealtedPerson { get; set; } public virtual RelationshipType RelationType { get; set; } public virtual Persons RealtedTo { get; set; } } </code></pre> <p>This gave three tables where the table Relations has two foreing keys towards Persons and one foreign key towards RelationshipType. No if I remove a person the relation including this person maybe removed as well. </p> <p>One final question. Why did you opted for an enum rather a class resulting in a table?</p> <p>Thanks again for your help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload