-1

There is a post that says how to do this but it does not explain how to work with it.

We have a class "Persoon" (dutch) in English 'Person' and we want to add a friend (vriend in Dutch) with an ICollection who's also a 'Person'

We have the following code in our domain class Persoon:

 public virtual ICollection<Persoon> Vrienden { get; set; }

this is because one person can have many friends (vrienden in Dutch)

For implementing our Icollection and creating our friendship table we have used the following code:

 modelBuilder.Entity<Persoon>()
             .HasMany(m => m.Vrienden)
             .WithMany()
             .Map(w => w.ToTable("Vriendschap")
                        .MapLeftKey("PersoonID")
                        .MapRightKey("VriendID"));

Now is the question, we have created the friendship table but how can we now add Person's to the friendship table with PersoonID (personID) & vriendID (friendID). I know that you can use 'vrienden.Add(entity)' but I don't know how to use ith within a new controller like friendshipcontroller is this not necessary ?

How do we have to implement this in our controllers and views ? so that we can add, edit, delete en see a list from the collection ?

Thanks in advance!!

tereško
  • 58,060
  • 25
  • 98
  • 150
Dolly93
  • 46
  • 5
  • I don't think what you're asking is clear. What exactly are you wanting to do in your controllers and views that you don't already understand? Are you asking someone to implement some controllers and views that provide CRUD functionality for friendships? – Sam Feb 18 '14 at 09:44
  • What does *list from the collection* mean? – Sam Feb 18 '14 at 09:44
  • @Sam Well you could say that, I don't know how to make the CRUD functionality for this Icollection. Do I have to make a new controller for the actions ? How can add friends to a person's friend collection ? I Hope this will help a bit ? – Dolly93 Feb 18 '14 at 09:47
  • @Sam I mean a list or table that displays all the values from the collection with the "Person" - "Friend" next to each other – Dolly93 Feb 18 '14 at 09:48
  • Can you update the question's tags to show which ORM tool you're using? – Sam Feb 18 '14 at 10:03

1 Answers1

1

CRUD

You can apply CRUD operations using code like the example you gave in your question:

Create

person.Vrienden.Add(friend);

Read

person.Vrienden;

Update

person.Vrienden.Remove(friend);
otherPerson.Vrienden.Add(friend);

Delete

person.Vrienden.Remove(friend);

Direct access

However, since you want to be able to query for all friendships, perhaps what you're wanting is to materialise the Friendship concept in your domain model:

public class Friendship
{
    public Persoon FriendA { get; set; }
    public Persoon FriendB { get; set; }
}

Adjust Persoon and its mapping so that Vrienden is an ICollection<Friendship>. Then adjust your mapping to be one-to-many (one person has many friendships).

Now you can query for all friendships and apply CRUD operations to Friendships directly. Note that you'll need to be careful to prevent duplicate friendships since you could have two friendships between the same pair of friends but with reversed order.

Sam
  • 40,644
  • 36
  • 176
  • 219