0

I have the following scenario to solve, using entity framework and a database first approach: A table Student having the fields:

ID (PK), Name, Surname

And a table StudentsFriends that basically tells me what students are related to another, having the fields:

StudentID, FriendID

For instance, the latter table content could be:

StudentID, FriendID,
1          2
1          3
2          3
2          1

What I'd like to achieve is creating an association with a navigation property on the Student entity which tells me the list of friends, using the vs built in model browser. The generated entity would look something like this:

public class Student
{
     public int ID;
     public string Name;
     public string Surname;
     public ICollection<Student> Friends;
}

In the example above Student 1 is Friends with Student 2, Student 3 and Student 2 is friends with Student 3, Student 1. Student 3 has no friends.

However I have no idea how to achieve that: I only managed to create association between entities, and if I create an association on the same entity I can't "reference" the table to join them.

Thansk in advance for the help.

Filippo Vigani
  • 904
  • 1
  • 9
  • 22
  • Here Friend and Student table has many-many relation with a mapping table StudentaFriends. So in Student class you should have ICollection and in Friend class you should have ICollection – Developer Aug 18 '16 at 17:19
  • That does not answer my question. – Filippo Vigani Aug 19 '16 at 08:03
  • May be I read it wrong; do you mean you have just single table Student? – Developer Aug 19 '16 at 08:30
  • No, I have two tables but I'm not asking what the classes should look like, I'm asking how to create the association in the visual studio model browser (edit the edmx file) in order to get the student class to work as intended – Filippo Vigani Aug 19 '16 at 08:41
  • If you wish to proceed via database first approach, then make the necessary changes in database and right click on edmx file -> Update model from database. Build the solution and this will update your model classes and edmx file. If that dint update, right click on edmx (in solution explorer) and do "Run custom tool" option – Developer Aug 19 '16 at 08:58
  • My question asks for what "the necessary changes" are – Filippo Vigani Aug 19 '16 at 11:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121344/discussion-between-developer-and-filippo-vigani). – Developer Aug 19 '16 at 11:56

0 Answers0